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,[]));
  1 件のコメント
James Brown
James Brown 2020 年 12 月 18 日
Excellent. Thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating 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