[R] an array of matrices

Petr Klasterecky klaster at karlin.mff.cuni.cz
Mon May 21 10:58:36 CEST 2007


Scott Hyde napsal(a):
> I'd like to have a three dimensional array of matrices.   I thought I could construct a five dimensional array to have the three dimensional array of matrices.  However, not all of the matrices in the array have the same dimensions, which seems to mean I can't use a five dimensional array.
> 
> What I'd like is this:
> 
> A = matrix(1:4,2,2)
> B = matrix(1:25,5,5)
> C = matrix(1,3,3)
> D = matrix(1,4,4)
> 
> I'd like to construct an array for which, if I type F[1,1], it returns matrix A, type F[1,2] and it returns B, type F[2,1] and it returns C, or type F[2,2] and it returns D.
> 
> Essentially, I'd like to be able to access them like they were elements of a matrix.  Although this example is only a two dimensional array of matrices, I'd like it to also work with three dimensions also.
> 
> The only thing I thought of to try was to make an array of lists and store the matrices inside of an array of lists (where each matrix is stored as a list with one item).

Too complicated. Making it the other way around, i.e. a list of 
matrices, is much cleaner. You can then access the individual matrices 
using '[[', e.g. F[[23]]. I don't know why (if even) you need the 
matrix-like indexing but if you really do, you can create an array of 
indeces like this:

ind.arr <- matrix(1:25, ncol=5, byrow=TRUE)
F[[(ind.arr[5,3])]]#is the same as F[[23]] above if F-matrix were 5x5.

Petr

> 
> Any suggestions?
> 
> -Scott
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

-- 
Petr Klasterecky
Dept. of Probability and Statistics
Charles University in Prague
Czech Republic



More information about the R-help mailing list