Cumulative sum at over an specified interval

7 ビュー (過去 30 日間)
Karina Gutierrez
Karina Gutierrez 2017 年 2 月 24 日
コメント済み: Beder 2017 年 3 月 3 日
Hi, I have an input vector of data that I would like to process to have a cumulative sum that resets and starts every 7 data points. For instance consider I have the following:
A = 1,0,2,2,4,1,0,7,2,3,1,0,0,1
I want to create a cumulative sum for the first 7 points and then start over again for the next set of 7 points, then I have:
B = 1,1,3,5,9,10,10,7,9,12,13,13,13,14
Thanks in advance! Karina

採用された回答

Beder
Beder 2017 年 2 月 24 日
B=reshape(A,7,[]);
C=cumsum(B,1);
D=reshape(C,[],1);
Or:
B=reshape(cumsum(reshape(A,7,[]),1,[],1);
Be careful: This only works as long as the length of A is divisible by 7.
  2 件のコメント
Karina Gutierrez
Karina Gutierrez 2017 年 2 月 27 日
That worked great! However, my datasets are variable in size and not all have a length that is divisible by 7. Is there a way to change the length of A to the nearest length divisible by 7? For example, if I have an array of length 15, I can make a code to omit the last point and reshape A to size 14?
Else I am going to have to go manually deleting data to make it the right length?
Thanks again for your help!
Beder
Beder 2017 年 3 月 3 日
Try to use "B=reshape(A(1:end-mod(numel(A),7)),7,[]); This will shorten your A to a size divisible by 7.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by