multiplication of a 3-D matrix by a 1-D array

1 次查看(过去 30 天)
I have a 3-D matrix A = rand(sz1,sz2,sz3) and a 1-D array B = rand(1,sz3).
Is there a biult-in function to do the muliplication like:
C with dimensions of (sz1,sz2,sz3)
for ii = 1:sz3
C(:,:,ii)=A(:,:,ii)*B(ii);
end

采纳的回答

Stephen23
Stephen23 2022-10-8
You don't need a special kind of multiplication, you just need to match the vector orientation to the array:
C = A .* reshape(B,1,1,[])
  2 个评论
yi yang
yi yang 2022-10-8
Thanks for your reply. It works.
I checked the matlab documentation for .* and did not find an answer for the question below.
In your answer, the 2 matrices have different dimensions and the element-wise multiplication works, why?
Stephen23
Stephen23 2022-10-11
编辑:Stephen23 2022-10-11
"I checked the matlab documentation for .* and did not find an answer for the question below."
The TIMES documentation states:
"In your answer, the 2 matrices have different dimensions and the element-wise multiplication works, why?"
Clicking the link given in the TIMES documentation takes you to this page:

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by