Sum the elements of an array by sequence

5 vues (au cours des 30 derniers jours)
Steven Shaaya
Steven Shaaya le 10 Déc 2021
Commenté : Steven Shaaya le 10 Déc 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

Réponse acceptée

Walter Roberson
Walter Roberson le 10 Déc 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 commentaire
Steven Shaaya
Steven Shaaya le 10 Déc 2021
Walter, you are a livesaver every single time. Thank you

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by