在Matlab滚动总和 [英] Rolling sum in Matlab
本文介绍了在Matlab滚动总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有作为的价格为每个日期列的截阵。我想创建一个具有30天的窗口滚动总和另一个数组。我想preFER不使用一个for循环。
目前,我用下面的code的一列:
I have an cross-sectional array with columns as prices for each date. I want to create another array with a rolling sum for window of 30 days. I would prefer not to use a for loop. Currently, I am using the following code for one column:
for i=31:n
for j=i-30:i-1
x = x + y(j)
end
sum(i) = x
end
我需要这对所有的证券,因此,code将不得不为数据集中的所有元素多次运行。
I need to this for all securities and thus the code will have to run multiple times for all elements in a dataset.
推荐答案
使用 cumsum
的!结合
data = 1:10;
n = length(data);
window = 3;
rollSum = cumsum(data)-[zeros(1,window), cumsum(data(1:n-window))];
rollSum =
1 3 6 9 12 15 18 21 24 27
1=1
1+2=3
1+2+3=6
2+3+4=9
3+4+5=12
这篇关于在Matlab滚动总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文