Borrar filtros
Borrar filtros

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

3 visualizaciones (últimos 30 días)
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 comentarios
Hancheng Zhu
Hancheng Zhu el 20 de Nov. de 2023
Your code is very good and concise, bro. But it spends more time than the latter answer below. Thanks for your kindly help.
Matt J
Matt J el 20 de Nov. de 2023
@Hancheng Zhu If that means you've settled on one of the answers below, please Accept-click one of them.

Iniciar sesión para comentar.

Respuesta aceptada

Bruno Luong
Bruno Luong el 17 de Nov. de 2023
Editada: Bruno Luong el 17 de Nov. de 2023
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);

Más respuestas (1)

Matt J
Matt J el 17 de Nov. de 2023
C=tensorprod(A,b,3,1)

Categorías

Más información sobre Mathematics en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by