2016-08-09 66 views
0
%In MATLAB: I have stored vectors A and B in two multidim. arrays: 
A = rand(4,1,4); B= rand(1,3,4); 

%Now I want to create multidim array C(3,3,4) 
%by multiply A and B in each dim i (:,:,i) without a for-loop. 

%So instead of these four operations below, just perform one operation e.g. C=A*B. 

C(:,:,1)=A(:,:,1)*B(:,:,1); 
C(:,:,2)=A(:,:,2)*B(:,:,2); 
C(:,:,3)=A(:,:,3)*B(:,:,3); 
C(:,:,4)=A(:,:,4)*B(:,:,4); 

我試過bsxfun,但找不到任何合適的操作。將存儲在兩個多維數組中的矢量相乘而無需for循環在Matlab中?

回答

2

您可以使用數組bsxfun

C = bsxfun(@times,A,B) 
+0

這是對我一個新的繁殖。在我弄清楚發生了什麼之前,我必須看兩遍。非常好。 – beaker

+0

哦,我試過了,但B'換了。太蠢了,謝謝。 –

相關問題