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

for loopnestedvectorization

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!

Best Answer

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