在Matlab中将多个向量存储在两个多维数组中,而不是for循环?

时间:2016-08-09 17:15:18

标签: arrays matlab multidimensional-array

%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,但找不到合适的操作。

1 个答案:

答案 0 :(得分:2)

您可以将数组乘以bsxfun

C = bsxfun(@times,A,B)