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!