简体   繁体   中英

R matlab package: why is repmat inconsistent?

I have got a question regarding the matlab package for R. Here's what I get

library(matlab)
a = matrix(1:4,2,2)
repmat(a,3,1)
     [,1] [,2]
[1,]    1    2
[2,]    3    4
[3,]    1    2
[4,]    3    4
[5,]    1    2
[6,]    3    4

this is what I expect. replicate a three times along the first dimension. but

b = matrix(1:6,2,3)
b
    [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

repmat(b,3,1)
      [,1] [,2]
[1,]    1    2
[2,]    3    4
[3,]    5    6
[4,]    1    2
[5,]    3    4
[6,]    5    6
[7,]    1    2
[8,]    3    4
[9,]    5    6

this is not consistent. I want a 6 by 3 matrix as the one obtained by

rbind(b,rbind(b,b))
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
[3,]    1    3    5
[4,]    2    4    6
[5,]    1    3    5
[6,]    2    4    6

It just appears to be transposing the matrix before doing the stacking. You could just transpose your matrix before sending it into repmat

> repmat(t(b), 3, 1)
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
[3,]    1    3    5
[4,]    2    4    6
[5,]    1    3    5
[6,]    2    4    6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM