Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to have a matrix of matrices in R?

Tags:

r

is it possible to have a matrix of matrices in R? if yes, how should I define such matrix? for example to have a 10 x 10 matrix, and each element of this matrix contains a matrix itself.

like image 924
nooshinha Avatar asked Jun 10 '15 10:06

nooshinha


1 Answers

1) list/matrix Yes, create a list and give it dimensions using matrix:

m <- matrix(1:4, 2)
M <- matrix(list(m, 2*m, 3*m, 4*m), 2)

so element 1,1 of M is m:

> M[[1,1]]
     [,1] [,2]
[1,]    1    3
[2,]    2    4

2) list/dim<- This also works:

M <- list(m, 2*m, 3*m, 4*m)
dim(M) <- c(2, 2)

3) array This is not quite what you asked for but depending on your purpose it might satisfy your need:

A <- array(c(m, 2*m, 3*m, 4*m), c(2, 2, 2, 2)) # 2x2x2x2 array

so element 1,1 is:

> A[1,1,,]
     [,1] [,2]
[1,]    1    3
[2,]    2    4
like image 141
G. Grothendieck Avatar answered Sep 20 '22 10:09

G. Grothendieck