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

1 回表示 (過去 30 日間)
Bidsitlov
Bidsitlov 2016 年 5 月 28 日
コメント済み: Bidsitlov 2016 年 5 月 28 日
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!

採用された回答

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by