mkv
mkv

Reputation: 23

Summing up till a certain interval

How can I compute the summation of an interval. I will use Matlab's code for eg.

data=[1;2;3;4;5;6;7;8;9;10;11;12]

I would like to perform this summation.

sum(1)=data(1)+data(2)+data(3)
sum(2)=data(4)+data(5)+data(6)
sum(3)=data(7)+(data(8)+data(9)
sum(4)=data(10)+data(11)+data(12)

How can I get about this? (Using for loop)

Upvotes: 1

Views: 802

Answers (1)

bla
bla

Reputation: 26069

No for loop needed, if indeed this interval is constant like in your example:

Ans=sum(reshape(data,3,[]))

note that I reshape the vector data to a matrix that has the right number of columns, so the value 3 relates to the interval size you wanted...

Upvotes: 7

Related Questions