[R] multidimensional lists
Evan Cooch
cooch17 at verizon.net
Wed Sep 27 19:46:35 CEST 2006
Gabor Grothendieck wrote:
> Just one other comment if the matrices have the same dimensions:
Indeed - that is quite often the case - thanks for the further
suggestion(s).
> they could alternately be represented as a 4d array:
>
> A <- matrix(1:4, 2)
>
> AA <- matrix(list(A, 10*A, 100*A, 1000*A), 2, byrow = TRUE)
> AA[1,2]
>
> AAA <- array(unlist(AA), c(2,2,2,2))
> AAA[,,1,2] # same
>
> This could have an advantage if you need to do things like
> easily take the top left element of each matrix,
>
> AAA[1,1,,]
>
> which in terms of AA would have required the longer expression:
>
> replace(AA, TRUE, lapply(AA, "[", 1, 1))
>
>
> On 9/27/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
>> Try this:
>>
>> AA <- matrix(list(A, 10*A, 100*A, 1000*A), 2, byrow = TRUE)
>> AA[1,2]
>>
>>
>> On 9/27/06, Evan Cooch <cooch17 at verizon.net> wrote:
>> > In the process of moving a number of my scripts from MATLAB -> R, I've
>> > discovered that there is no 'pure' equivalent of MATLAB's cell arrays,
>> > which I use quite often. Basically, I create matrices (as a cell
>> array)
>> > where each element of the matrix is itself a matrix (e.g., 2x2 cell
>> > array where each element of the array is another matrix). I pass these
>> > cell arrays to various functions which then do clever things with the
>> > various matrices (of course) - basically, I need to be able to pass
>> > collections of matrices to functions to do various things, and I
>> need to
>> > be able to control the dimensionality of the cell array to preserve
>> some
>> > structural relationships among the matrices in the array. The cell
>> array
>> > in MATLAB handles this with aplomb.
>> >
>> > So far, in R, I've used lists. Given (say) 4 matrices (A,B,C,D), in
>> > MATLAB I can use
>> >
>> > test={A,B,C,D} for a row vector cell array, or
>> >
>> > test={A;B;C;D} for a column vector cell array.
>> >
>> > In R, I get more or less the same thing using
>> >
>> > test=list(A,B,C,D)
>> >
>> > but this only gives me a row list. For a bunch of technical reasons, I
>> > need to be able to control the orientation (as noted)- this is
>> > especially true for n-dimensional cell arrays. In MATLAB, for
>> example, I
>> > could generate a (say) 2x2 cell array using
>> >
>> > test={A B;C D}
>> >
>> > The only way I can figure out how to do this in R is using
>> something like
>> >
>> > test=list(A,B,C,D);
>> > dim(test) < c(2,2);
>> >
>> > This seems to work, but defaults to bycolumn (in other words,
>> instead of
>> >
>> > A B
>> > C D
>> >
>> > I get
>> >
>> > A C
>> > B D
>> >
>> > )
>> >
>> > So, I follow with
>> >
>> > test=t(test) as needed to flip the thing around to byrow.
>> >
>> > OK, so the question is - is there a better way? This *seems* to work,
>> > but I'm discovering that R is a lot like working with LaTeX
>> (something I
>> > know much more about) - you can do most things, but there is often a
>> > more elegant way if you can figure out how to find out about it.
>> >
>> > Thanks in advance...
>> >
>> > ______________________________________________
>> > 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.
>> >
>>
>
More information about the R-help
mailing list