Vectorization of a nested for loop, where inner counter depends on outer one.
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Bidsitlov
el 28 de Mayo de 2016
Comentada: Bidsitlov
el 28 de Mayo de 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 comentarios
Respuesta aceptada
Stephen23
el 28 de Mayo de 2016
>> [X,Y] = meshgrid(3:6);
>> idx = Y>=X;
>> sum(factorial(Y(idx)) ./ factorial(Y(idx)-X(idx)))
ans =
2274
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!