How can i vectorize a matrix multiplication of higher dimension arrays?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I want to multiply a 6x6 matrix A and a 6x1 vector B at every grid point in a 3D earth model. Currently I am doing this:
for ix=1:length(x)
for iy=1:length(y)
for iz=1:length(z)
C(:,ix,iy,iz)=squeeze(A(:,:,ix,iy,iz))*squeeze(B(:,ix,iy,iz));
end
end
end
Is there a way to vectorize this? Currently this segment of nested loops is by far the slowest part of my code (almost 90% of the time taken every iteration is spent in this nested loop). Thanks!
Antworten (2)
Steven Lord
am 17 Sep. 2020
If you're using release R2020b or later, take a look at the pagemtimes function introduced in that release.
0 Kommentare
Matt J
am 1 Apr. 2016
C=mtimesx( reshape(A,6,6,[]) , reshape(B,6,1,[]) );
C=reshape(C,size(B));
0 Kommentare
Siehe auch
Kategorien
Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!