Matlab - building a matrix by merging the same raw vector several times

Is there a matlab function that allows me to perform 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] m = [1 2 2 3; 1 2 2 3; 1 2 2 3; 1 2 2 3]

+7
source share
2 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 lines:

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

or even an external product:

 m = ones(4,1)*x; 

and also using BSXFUN:

 m = bsxfun(@times, x, ones(4,1)) 
+12
source

You can try using vertcat , for example:

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

Or even just:

 x = [1 2 2 3]; m = [x;x;x;x]; 

EDIT:

for multiples of x, you can do:

 x = [1 2 2 3]; m = [x;2*x;3*x]; % [1 2 2 3; 2 4 4 6; 3 6 6 9] 

EDIT2:

For an arbitrary number x in m ...

 n = 3; % number of repetitions... x = [1 2 2 3]; m = []; for i=1:n m = [m;x]; end 
0
source

All Articles