[R] Odp: Need help on matrix manipulation

Petr PIKAL petr.pikal at precheza.cz
Mon Mar 29 09:56:37 CEST 2010


Hi

One option (probably not the best one) is index an aggregate matrix

mat <- rbind(mat1, mat2, mat3)
idx <- seq(1,12, 4)+rep(0:3, each=3)
mat <- mat[idx, ]

Regards
Petr


r-help-bounces at r-project.org napsal dne 29.03.2010 09:44:12:

> 
> Dear all,
> 
> Ket say I have 3 matrices :
> 
> mat1 <- matrix(rnorm(16), 4)
> mat2 <- matrix(rnorm(16), 4)
> mat3 <- matrix(rnorm(16), 4)
> 
> Now I want to merge those three matrices to a single one with dimension
> 4*3=12 and 4 wherein 
> 
> on resulting matrix, row 1,4,7,10 will be row-1,2,3,4 of "mat1", row
> 2,5,8,11 will be row-1,2,3,4 of "mat2" and row 3,6,8,12 will be 
row-1,2,3,4
> of "mat3". For example suppose the matrices are :
> 
> > mat1
>            [,1]       [,2]        [,3]        [,4]
> [1,] -0.3597661 -0.8725675  0.92033577  0.42461934
> [2,] -1.0431990 -0.5221843  0.31821494 -0.01963307
> [3,]  0.4736285 -1.3917844  0.03776291  0.52503899
> [4,]  0.2341584  0.2959110 -0.33166146  1.22208331
> 
> > mat2
>            [,1]       [,2]       [,3]       [,4]
> [1,]  1.0666719 -1.0310117 -1.4418511 -0.2461388
> [2,]  2.0099067 -0.3866974 -0.2978510  0.9377186
> [3,] -1.5532591 -0.1341572 -1.0745971  0.1120120
> [4,]  0.4318782  0.1166596 -0.6320177 -0.4709091
> 
> > mat3
>             [,1]        [,2]       [,3]       [,4]
> [1,] -0.15092091  1.11900300  0.7173543  0.3631286
> [2,] -1.09844902  1.55239518 -0.5049884  0.8974597
> [3,] -1.60176717 -0.03991147 -1.2475162  1.4643000
> [4,] -0.06721017 -0.88329791 -1.9875210 -0.7989177
> 
> then the resulting matrix will be :
> 
> -0.3597661   -0.8725675   0.92033577   0.42461934
> 1.0666719   -1.0310117   -1.4418511   -0.2461388
> -0.15092091   1.119003   0.7173543   0.3631286
> -1.043199   -0.5221843   0.31821494   -0.01963307
> 2.0099067   -0.3866974   -0.297851   0.9377186
> -1.09844902   1.55239518   -0.5049884   0.8974597
> 0.4736285   -1.3917844   0.03776291   0.52503899
> -1.5532591   -0.1341572   -1.0745971   0.112012
> -1.60176717   -0.03991147   -1.2475162   1.4643
> 0.2341584   0.295911   -0.33166146   1.22208331
> 0.4318782   0.1166596   -0.6320177   -0.4709091
> -0.06721017   -0.88329791   -1.987521   -0.7989177
> 
> -- 
> View this message in context: http://n4.nabble.com/Need-help-on-matrix-
> manipulation-tp1694804p1694804.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.



More information about the R-help mailing list