Multiply one dimension of a 3D Matrix by a vector
31 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
James Brown
am 18 Dez. 2020
Kommentiert: James Brown
am 18 Dez. 2020
I wish to element multiply the third dimension of Matrix A by vector B by for all xy points. The following operation takes 329 seconds.
Is there an operation that avoids the for loops?
My Code:
Matrix A has dimensions A(2048,200,513)
Vector B has dimension(513)
C = zeros(numx,numy,numz);
for j = 1:numy
for i = 1:numx
Az = squeeze(A(i,j,:));
C(i,j,:) = squeeze(B.*Az);
end
end
0 Kommentare
Akzeptierte Antwort
James Tursa
am 18 Dez. 2020
Bearbeitet: James Tursa
am 18 Dez. 2020
C = A .* reshape(B,1,1,[]);
For earlier versions of MATLAB that do not have implicit expansion it would be this:
C = bsxfun(@times,A,reshape(B,1,1,[]));
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!