Eliminate for loop: can this code be vectorized?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Quinten Rensen
le 27 Juil 2018
Commenté : Quinten Rensen
le 28 Juil 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 commentaires
Réponse acceptée
KSSV
le 27 Juil 2018
Modifié(e) : KSSV
le 27 Juil 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!