# Re: [R] an array of matrices

From: Scott Hyde <hydes_at_byuh.edu>
Date: Mon, 21 May 2007 17:24:45 -1000 (HST)

Thanks for the replies Jim and Petr!

I decided to do it a slightly different way. Using Jim's method, the matrices needed to be created before the array, whereas I needed the array to be allocated first, and then filled. Here's how I did it:

This command creates a blank array of lists

> y=array(list(),c(i,j,k))

Then each item can be accessed (and assigned) using

> y[[i,j,k]]

For example:

> for (i in 1:2)

```+   for (j in 1:2)
+     for (k in 1:2)
+       y[[i,j,k]] <- diag(i+j+k)

```

> y
, , 1

[,1] [,2]
[1,] Numeric,9 Numeric,16
[2,] Numeric,16 Numeric,25

, , 2

[,1] [,2]
[1,] Numeric,16 Numeric,25
[2,] Numeric,25 Numeric,36

> y[[1,1,1]]

[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1

> y[[2,1,2]]

[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 0 0
[2,] 0 1 0 0 0
[3,] 0 0 1 0 0
[4,] 0 0 0 1 0
[5,] 0 0 0 0 1

Thanks again for the help!

-Scott

• Original message ---- >Date: Mon, 21 May 2007 04:57:40 -0400 >From: "jim holtman" <jholtman_at_gmail.com> >Subject: Re: [R] an array of matrices >To: "Scott Hyde" <hydes_at_byuh.edu> >Cc: r-help_at_stat.math.ethz.ch > > Create a matrix of list: > > > A = matrix(1:4,2,2) > > B = matrix(1:25,5,5) > > C = matrix(1,3,3) > > D = matrix(1,4,4) > > x <- list(A,B,C,D) > > dim(x) <- c(2,2) > > x[[1,1]] > [,1] [,2] > [1,] 1 3 > [2,] 2 4 > > x[[2,2]] > [,1] [,2] [,3] [,4] > [1,] 1 1 1 1 > [2,] 1 1 1 1 > [3,] 1 1 1 1 > [4,] 1 1 1 1 > > > On 5/21/07, Scott Hyde <hydes_at_byuh.edu> wrote: > > 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). > > 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. > > -- > Jim Holtman > Cincinnati, OH > +1 513 646 9390 > > What is the problem you are trying to solve?

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. Received on Tue 22 May 2007 - 03:34:45 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 22 May 2007 - 05:31:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.