Vectorization of a nested for loop, where inner counter depends on outer one.

1 vue (au cours des 30 derniers jours)
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!

Réponse acceptée

Stephen23
Stephen23 le 28 Mai 2016
>> [X,Y] = meshgrid(3:6);
>> idx = Y>=X;
>> sum(factorial(Y(idx)) ./ factorial(Y(idx)-X(idx)))
ans =
2274

Plus de réponses (0)

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!

Translated by