[R] How to create a matrix with 3 dimensions from several 2 dimensional matrice?

jim holtman jholtman at gmail.com
Tue Dec 27 18:29:32 CET 2011


Is this what you are after:

> x1 <- matrix(1:25, 5)
> x2 <- x3 <- x1
>
> # combine matrices, redimension and then rename
> z <- cbind(x1, x2, x3)
> dim(z) <- c(5, 5, 3)
> z  # without names
, , 1

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16   21
[2,]    2    7   12   17   22
[3,]    3    8   13   18   23
[4,]    4    9   14   19   24
[5,]    5   10   15   20   25

, , 2

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16   21
[2,]    2    7   12   17   22
[3,]    3    8   13   18   23
[4,]    4    9   14   19   24
[5,]    5   10   15   20   25

, , 3

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16   21
[2,]    2    7   12   17   22
[3,]    3    8   13   18   23
[4,]    4    9   14   19   24
[5,]    5   10   15   20   25

> dimnames(z) <- list(NULL, NULL, c('x1', 'x2', 'x3')) # object names
> z
, , x1

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16   21
[2,]    2    7   12   17   22
[3,]    3    8   13   18   23
[4,]    4    9   14   19   24
[5,]    5   10   15   20   25

, , x2

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16   21
[2,]    2    7   12   17   22
[3,]    3    8   13   18   23
[4,]    4    9   14   19   24
[5,]    5   10   15   20   25

, , x3

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16   21
[2,]    2    7   12   17   22
[3,]    3    8   13   18   23
[4,]    4    9   14   19   24
[5,]    5   10   15   20   25


On Tue, Dec 27, 2011 at 10:34 AM, ali_protocol
<mohammadianalimohammadian at gmail.com> wrote:
> Hi every  one,
>
> How is it possible  to create a matrix with 3 dimensions from several 2
> dimensional matrice?
> Is it possible that each of "elementary/building block" matrices could be
> called by its corresponding  original name?
>
> Thanks alot.
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-create-a-matrix-with-3-dimensions-from-several-2-dimensional-matrice-tp4237360p4237360.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org 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
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list