Summing up to a certain interval

How can I calculate interval summation. I will use Matlab code, for example.

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 do this? (Use for loop)

+1
source share
1 answer

Not for the loop, if it really is like in your example:

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

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

+7
source

Source: https://habr.com/ru/post/1502139/


All Articles