sum of complicated numbers

조회 수: 3 (최근 30일)
Joseph
Joseph 2022년 2월 15일
댓글: Image Analyst 2022년 2월 15일
how can I add these numbers
1:2:101 , 1:-2:101
  댓글 수: 3
DGM
DGM 2022년 2월 15일
... it used to be an alternating series of odd integers from 1 to 101
Image Analyst
Image Analyst 2022년 2월 15일
Do you mean he had said previously
1 -3 5 -7 9 -11 13 -15 17 -19 21
and then changed it to the null vector 1 : -2 : 101 ?

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

답변 (1개)

DGM
DGM 2022년 2월 15일
편집: DGM 2022년 2월 15일
Consider the reduced example.
N = 21;
n = 1:2:N
n = 1×11
1 3 5 7 9 11 13 15 17 19 21
n = (1-2*mod((n-1)/2,2)).*n
n = 1×11
1 -3 5 -7 9 -11 13 -15 17 -19 21
s = sum(n)
s = 11
If you must absolutely do it with a loop, you can use the above arithmetic to generate the sequence.
EDIT:
The revised question does suggest its own method which works just as well:
N = 21;
n = sum([1:4:N -(3:4:N)])
n = 11

카테고리

Help CenterFile Exchange에서 Electrical Block Libraries에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by