Multiply one dimension of a 3D Matrix by a vector

조회 수: 31 (최근 30일)
James Brown
James Brown 2020년 12월 18일
댓글: James Brown 2020년 12월 18일
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

채택된 답변

James Tursa
James Tursa 2020년 12월 18일
편집: James Tursa 2020년 12월 18일
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,[]));

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by