Eliminate for loop: can this code be vectorized?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Quinten Rensen
el 27 de Jul. de 2018
Comentada: Quinten Rensen
el 28 de Jul. de 2018
Hi all,
I am trying to optimize my code, my question is if it is possible to vectorize the following code:
for r = 1:2:(N-1)
U(r,1)=U(r,1)+ (1./K(r,r)).*(F(r,1)-K(r,:)*U(:,1));
end
where U and F are Nx1 vectors and K is a NxN matrix. This part of the code takes a considerable amount of time when N becomes large. Maybe someone has a suggestion on how to vectorize this part?
Thank you!
0 comentarios
Respuesta aceptada
KSSV
el 27 de Jul. de 2018
Editada: KSSV
el 27 de Jul. de 2018
r = 1:2:(N-1) ;
U(r,1)=U(r,1)+ (1./K(r,r))*(F(r,1)-K(r,:)*U(:,1));
unchecked check the values before using.
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!