总结到一定间隔 [英] Summing up till a certain interval

查看:107
本文介绍了总结到一定间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算间隔的总和.我将使用Matlab的代码作为示例.

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]

我想进行这种求和.

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)

推荐答案

不需要 for循环,如果该间隔确实是恒定的,例如您的示例:

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

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

请注意,我将向量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...

这篇关于总结到一定间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆