Vectorization of a nested for loop, where inner counter depends on outer one.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Bidsitlov
am 28 Mai 2016
Kommentiert: Bidsitlov
am 28 Mai 2016
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 Kommentare
Akzeptierte Antwort
Stephen23
am 28 Mai 2016
>> [X,Y] = meshgrid(3:6);
>> idx = Y>=X;
>> sum(factorial(Y(idx)) ./ factorial(Y(idx)-X(idx)))
ans =
2274
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!