Sum the elements of an array by sequence

5 Ansichten (letzte 30 Tage)
Steven Shaaya
Steven Shaaya am 10 Dez. 2021
Kommentiert: Steven Shaaya am 10 Dez. 2021
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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 10 Dez. 2021
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 Kommentar
Steven Shaaya
Steven Shaaya am 10 Dez. 2021
Walter, you are a livesaver every single time. Thank you

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by