Sum the elements of an array by sequence

조회 수: 5 (최근 30일)
Steven Shaaya
Steven Shaaya 2021년 12월 10일
댓글: Steven Shaaya 2021년 12월 10일
I have an array 'X' with size 10000.
For the porspose of understanding this problem let's say that my X is
X=[1 4 15 6 7 5 3 2 11 13 1 7 8 13 2 5 9 3 2 11 13 1 7 8 6 7 5 13 5 9] which has 30 elements
Now, I want to add the first 5 elements togather and the second 5 elements togather and the third 5 elements togather and so on.
I want to display the new array on Y
I am not sure how to let matlab do that for me so I do not have sum them manually of use the tedious method such us
Y1=sum(X(1,5))
Y2=sum(X(6,10))
Y2=sum(X(11,15))
.
.
.
.
.
Y6=sum(X(26,30))
So that my
Y=[Y1 Y2 Y3 Y4 Y5 Y6]
Can you help me on this please

채택된 답변

Walter Roberson
Walter Roberson 2021년 12월 10일
X=[1 4 15 6 7 5 3 2 11 13 1 7 8 13 2 5 9 3 2 11 13 1 7 8 6 7 5 13 5 9]
X = 1×30
1 4 15 6 7 5 3 2 11 13 1 7 8 13 2 5 9 3 2 11 13 1 7 8 6 7 5 13 5 9
sum(reshape(X, 5, []))
ans = 1×6
33 34 31 30 35 39
  댓글 수: 1
Steven Shaaya
Steven Shaaya 2021년 12월 10일
Walter, you are a livesaver every single time. Thank you

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by