Skip to content
Prev 18169 / 398502 Next

repeating rows or columns within a matrix

Juhana> Hello
    Juhana> Spse I have a matrix, say

    Juhana> 1 2 3
    Juhana> 4 5 6
    Juhana> 7 8 9

    Juhana> and I would like to expand it by repeating rows within the matrix, to
    Juhana> get, if the repeating factor is 2, say:

    Juhana> 123
    Juhana> 123
    Juhana> 456
    Juhana> 456
    Juhana> 789
    Juhana> 789

    Juhana> (or columnwise as well) . There must be a smart way of doing that?

Juhana,

you could try the kronecker product
[,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
[,1] [,2] [,3]
[1,]    1    2    3
[2,]    1    2    3
[3,]    4    5    6
[4,]    4    5    6
[5,]    7    8    9
[6,]    7    8    9

Cheers,

Andreas