Vectorization of a nested for loop, where inner counter depends on outer one.
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hi, I'm trying to vectorize the following code:
for i=3:6
for j=i:6
howmuch = howmuch + factorial(j) / factorial(j - i);
end
end
I tried the meshgrid approach, however couldn't find a way to connect "j to i". How can I do this?
Thanks!
0 Commenti
Risposta accettata
Stephen23
il 28 Mag 2016
>> [X,Y] = meshgrid(3:6);
>> idx = Y>=X;
>> sum(factorial(Y(idx)) ./ factorial(Y(idx)-X(idx)))
ans =
2274
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!