loop iteration in matlab
3 ビュー (過去 30 日間)
古いコメントを表示
I have the following polynomial
F(x) = 1^5 + (1^5 + 2^5)+ (1^5 + 2^5 + 3^5) + ... + (1^5 + 2^5 + 3^5 + 4^5 + ... + x^5)
How can I write a loop iteration for this polynomials?
1 件のコメント
Stephen23
2020 年 11 月 20 日
Why do you need a loop?
1^5 + (1^5 + 2^5) + (1^5 + 2^5 + 3^5) + (1^5 + 2^5 + 3^5 + 4^5)
sum(cumsum((1:4).^5))
採用された回答
その他の回答 (1 件)
Rik
2020 年 11 月 20 日
I would suggest not using a loop:
F=@(x) sum( ( (1:x).^5 ).*(x:-1:1) );
2 件のコメント
Rik
2020 年 11 月 20 日
Yes, and the code posted by Stephen in a comment is also vectorized.
There are probably some exceptions I'm not thinking of, but if you don't see a loop (or cellfun/arrayfun) the code is vectorized.
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!