1

I have matrix

A = [1;2;3]

How do I replicate A four times, replicating each row four times before moving onto the next, to get

[1;1;1;1;2;2;2;2;3;3;3;3;4;4;4;4]

?

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
user1956609
  • 2,132
  • 5
  • 27
  • 43
  • 1
    A fairly comprehensive list comparing many solutions to this problem can be found here: http://stackoverflow.com/questions/14615305/a-similar-function-to-rs-rep-in-matlab – Dan Jan 14 '14 at 06:45
  • Note since 2015a the best method is to use the builtin [`repelem`](http://www.mathworks.com/help/matlab/ref/repelem.html) function – Dan Jun 02 '16 at 14:10

2 Answers2

4

In this particular instance, you could do something along the lines of

A = [1;2;3;4];
B = repmat(A',4,1);
B = B(:);

What this does is replicate A' to create a matrix B:

 1     2     3     4
 1     2     3     4
 1     2     3     4
 1     2     3     4

It then converts it to a single column using B(:).

MrAzzaman
  • 4,734
  • 12
  • 25
2

How about using kron? It's perfect for this.

kron(A,ones(4, 1))
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70