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

橙三吉。 提交于 2019-11-26 23:16:17

问题


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]


回答1:


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))



回答2:


You could try using vertcat, like this:

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

Or even simply:

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 of x's in m...

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


来源:https://stackoverflow.com/questions/6889367/matlab-building-a-matrix-by-merging-the-same-raw-vector-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!