Sum the elements of an array by sequence

7 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Walter Roberson
Walter Roberson il 10 Dic 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

Più risposte (0)

Categorie

Scopri di più su Graphics Object Programming in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by