1. Assume a vector V which is (P X 1) array.
  2. Assume matrices A,B which is (M X N X P) array.
I want to multiply A(i, j, :) .* V for each 0 < i <= M , and 0 < j <= N.
Currently I'm using loops to make it happen:
for ii = 1 : M
for jj = 1 : N
B(ii, jj, :) = V.* squeeze(A(ii, jj, :));
end
end
Is there any way to do so which doesn't include any use of loops?

1 comentario

madhan ravi
madhan ravi el 18 de Dic. de 2018
How about this?
a= rand(3,2,3); %M X N XP
V=rand(2,1); %P X 1
A=permute(a,[3 2 1])
A(:,:,1) * V % V multiplied with first page?

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 18 de Dic. de 2018
Editada: Jan el 18 de Dic. de 2018

2 votos

B = A .* reshape(V, [1, 1, numel(V)])
This works with auto-expanding since R2016b.

3 comentarios

Nitai Fingerhut
Nitai Fingerhut el 18 de Dic. de 2018
seems like it works!
thanks!
Matt J
Matt J el 18 de Dic. de 2018
@Nitai, you should Accept-click Jan's solution, since it solves your problem.
Jan
Jan el 18 de Dic. de 2018
You are welcome. Matlab makes it easy to suggest such a compact and efficient solution.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 18 de Dic. de 2018

Comentada:

Jan
el 18 de Dic. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by