-2

Are there any quick ways to populate each row of a matrix with the same row vector?

For example, suppose a vector like [ 1 2 3 ], I would like to quickly build a matrix like: [ 1 2 3; 1 2 3; 1 2 3; .... 1 2 3].

shapeare
  • 4,133
  • 7
  • 28
  • 39

1 Answers1

1

You want Matlab's B = repmat(A,sz1,sz2,...,szN). See http://www.mathworks.com/help/matlab/ref/repmat.html for details.

Given a matrix A = [1 2 3], you would do this:

B = repmat(A,4,1)

which means "replicate matrix A in 4 rows and 1 column".

and that would give you

B = [1 2 3
     1 2 3
     1 2 3
     1 2 3]
Andy Clifton
  • 4,926
  • 3
  • 35
  • 47