How to calculate sum without loop?

조회 수: 17 (최근 30일)
Numerical Noob
Numerical Noob 2021년 2월 12일
댓글: Numerical Noob 2021년 2월 12일
Hello,
I must calculate S in the range of n=1 to n=100 without a loop.
s=(1...100)(101-n)*cos(n/100)
Example= 100*cos(1/100) + 99*cos(2/100) ... + 1*cos(100/100)

답변 (1개)

KSSV
KSSV 2021년 2월 12일
n = 1:100 ;
s = sum(n.*cos(n/100))
  댓글 수: 2
Aditya Kommajosula
Aditya Kommajosula 2021년 2월 12일
편집: Aditya Kommajosula 2021년 2월 12일
If I understand the OP's question right, the solution might have to be:
n = 1:100;
s = sum(n(end:-1:1).*cos(n/100));
Regards
Numerical Noob
Numerical Noob 2021년 2월 12일
Thank you Aditya, I tried it with an iterative approach with n=1..3. Your solution is right.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by