请问是否有一种方法可以从一个一维数组生成一个二维数组,其中二维数组的行是通过重复一维数组中对应元素生成的。
例如:
1D array 2D array
|1| |1 1 1 1 1|
|2| |2 2 2 2 2|
|3| -> |3 3 3 3 3|
|4| |4 4 4 4 4|
|5| |5 5 5 5 5|
为了提供更多帮助,以下是我的一些建议:
设A = (1:5)'
Using indices [faster than repmat]:
B = A(:, ones(5,1))
Using matrix outer product:
B = A*ones(1,5)
Using bsxfun() [not the best way of doing it]
B = bsxfun(@plus, A, zeros(1,5))
%# or
B = bsxfun(@times, A, ones(1,5))
a = [1 2 3 4 5]'
l = size(a)
for i=2:5
a(1:5, i) = a(1:5)
循环只是不断将列附加到末尾。
A
情况。 - Amro