Sum the elements of an array by sequence
2 views (last 30 days)
Show older comments
Steven Shaaya
on 10 Dec 2021
Commented: Steven Shaaya
on 10 Dec 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
0 Comments
Accepted Answer
Walter Roberson
on 10 Dec 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]
sum(reshape(X, 5, []))
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!