Efficient multiplication between arrays without for loops

I have two arrays which need to be multiplied across individual elements of all rows and columns. This is simply achieved through the .* operator. However, in my scenario, both arrays I possess are three dimensional and my operation using for loops proceed as follows,
mass = 80;
for t=1:size(Vel,3)
for branch=1:size(Eigen,3)
Velocity=Vel(:,:,t);
Eig=Eigen(:,:,branch);
Q=Velocity.*conj(Eig).*((mass)^0.5);
Q_direction=sum(Q,2);
Q_atom=sum(Q_direction,1);
Q_branch(branch)=Q_atom;
end
Q_dot(t,:)=Q_branch;
end
Where I take the t(th) dimension of array 'Vel' and the branch(th) dimension of array 'Eigen' into two 2D arrays, and perform the multiplication between individual elements. However, I need to do the same process across all of the 3rd dimensions in both 'Vel' and 'Eigen' arrays. Is there a more efficient way to complete this without using for loops?
Note: Once the multiplication is obtained, the columns and rows of the arrays are summed. The data to be stored are based on the 3rd dimension of Array 'Vel' and 3rd dimension of Array 'Eigen' (Output is dependent only on t and branch).
Also let me know if the efficiency of this section can be improved in any other way. I am new to matlab and any sort of programming language, so thanks in advance for any support.

 采纳的回答

[v1,v2,v3]=size(Vel);
[e1,e2,e3]=size(Eigen);
Eig=reshape(Eigen,[e1,e2,1,e3]);
Q=Vel.*conj(Eig).*((mass)^0.5);
Q_dot=reshape( sum(Q,[1,2]) , v3,[] );

3 个评论

Thanks for the quick response, I didn't think of reshaping the matrices to reduce number of dimensions earlier. Also, is there a significant difference in performance between the following two executions?
  1. Q_dot(t,:,:)=Q_branch;
  2. Q_dot(:,:,t)=Q_branch;
Should I be concerned regarding the order in which I store arrays?
Sorry if this is not directly related to the earlier question.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品

版本

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by