How to efficiently calculate a weighted sum of 3 dimensional matrix through a vector in matlab

4 次查看(过去 30 天)
A is a 3 dimensional matrix (M*N*K), b is a vector with length K. I need to calculate
, which is a 2 dimensional matrix (M*N). But i don't want to write it by a explicit summation because it is too slow. Is there any way to calculate it efficiently through matlab? Please attach the code if convenience.
  3 个评论
Hancheng Zhu
Hancheng Zhu 2023-11-20
Your code is very good and concise, bro. But it spends more time than the latter answer below. Thanks for your kindly help.

请先登录,再进行评论。

采纳的回答

Bruno Luong
Bruno Luong 2023-11-17
编辑:Bruno Luong 2023-11-17
This uses matrix multiplication so it should be well optimized and fast.
[m,n] = size(A, [1 2]);
C = reshape(A,m*n, []) * B(:);
C = reshape(C, m, n);

更多回答(1 个)

Matt J
Matt J 2023-11-17
C=tensorprod(A,b,3,1)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by