Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a matrix by merging the same row vector multiple times

Is there a matlab function which allows me to do the following operation?

x = [1 2 2 3];

and then based on x I want to build the matrix m = [1 2 2 3; 1 2 2 3; 1 2 2 3; 1 2 2 3]

like image 579
Simon Avatar asked Jul 31 '11 12:07

Simon


1 Answers

You are looking for the REPMAT function:

x = [1 2 2 3];
m = repmat(x,4,1);

You can also use indexing to repeat the rows:

m = x(ones(4,1),:);

or even outer-product:

m = ones(4,1)*x;

and also using BSXFUN:

m = bsxfun(@times, x, ones(4,1))
like image 69
Amro Avatar answered Oct 10 '22 15:10

Amro