How to eliminate for-loop?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Andreas Schwager
el 27 de Jul. de 2016
Comentada: Andreas Schwager
el 1 de Ag. de 2016
Hi to everybody,
in the for-loop below, is a multiplication of a scalar with twice a matrix column. If two vectors in a for-loop are multiplied it should be possible to reformulate it into a matrix multiplication. How to do this? The task is to speed up the processing, especially if dimensions of matrix are becoming large.
A = [1 2; 3 4]
B = [5 6 7; 8 9 10; 11 12 13]
C = [1 0 0; 0 2 0; 0 0 0]
for indx = 1:size(A,2)
D(indx,:,:) = C(indx,indx) .* A(:,indx) * B(:,indx)';
end
Thanks Andy
0 comentarios
Respuesta aceptada
Andrei Bobrov
el 27 de Jul. de 2016
Editada: Andrei Bobrov
el 27 de Jul. de 2016
A = [1 2; 3 4];
B = [5 6 7; 8 9 10; 11 12 13];
C = [1 0 0; 0 2 0; 0 0 0];
n = size(A,2);
Cd = diag(C);
D = bsxfun(@times,A,reshape(Cd(1:n),1,[]));
D = bsxfun(@times,D,permute(B(:,1:n),[3,2,1]));
D = permute(D,[2,1,3]);
2 comentarios
Más respuestas (1)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!