2014-07-02 56 views

回答

2

有很多方法可以做到這一點,例如使用Kronecker tensor product這樣的:

B = kron(A,ones(1,3)) 

或組合reshaperepmat這樣的:

B = reshape(repmat(A,3,1),1,[]) 

,如果你如果還不能確定矢量是水平還是垂直,你可以使用(:)和轉置.'這樣:

B = kron(A(:).', ones(1,3)) 
B = reshape(repmat(A(:).',3,1),1,[]) 

編輯

你說的重塑版本沒有工作。這裏是我的測試結果:

A = 1:4 
B = reshape(repmat(A(:).',3,1),1,[]) 
B = 
    1 1 1 2 2 2 3 3 3 4 4 4 
A = (1:4)' 
B = reshape(repmat(A(:).',3,1),1,[]) 
B = 
    1 1 1 2 2 2 3 3 3 4 4 4 

所以,它適用於列和行向量(至少對我來說)。

一些更多的方式來做到這一點:

%# vector-matrix product 
reshape(ones(3,1)*A, 1,[]) 

%# reorganize simple concatenation 
nA = 3*numel(A);  
B([1:3:nA 2:3:nA 3:3:nA]) = [A A A]; 
+0

其中很多方面,其中之一是最快的? – user3482383

+0

我建議你用['timeit'](http://www.mathworks.se/help/matlab/ref/timeit.html)自己測試一下。如果你在測試過程中分享基準測試結果,這將是很好的=) –

+0

我測試了一個隨機數向量,包含1000個元素,重複因子爲300,kron函數的時間爲t1 = 0.0373,時間對於重塑函數是t2 = 0.0091。約快四倍 – user3482383