2014-07-02 53 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