Vectorizing a simple for loop
12 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi everyone, I'm not sure if people asked this before but I tried googling my question before posting.
so i know for this type of loop for example:
for i = 2:n-1
for j = 2:n-1
q(i,j) = a*u(i,j)
end
end
can be simply expressed as: q((2:n-1),(2:n-1)) = a*u((2:n-1),(2:n-1));
what if my expression was as follows: for i = 2:n-1 for j = 2:n-1 q(i,j) = a*u(i+1,j) end end
how could I incorporate the "i+1" expression into vectorizing.
Thank you for your help
0 Commenti
Risposta accettata
Jan
il 3 Dic 2011
q(2:n-1, 2:n-1) = a*u(3:n, 2:n-1);
4 Commenti
Jan
il 5 Dic 2011
The vectorization suffers from the need of large temporary arrays. Allocating memory for "q(2:(nx-1),2:(ny-1))" etc is very expensive, while the multiplication by a scalar is very cheap. Therefore the loop will be more efficient for large inputs.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!