How to sum up different values of a matrix?

3 ビュー (過去 30 日間)
Mato Lab
Mato Lab 2016 年 10 月 10 日
コメント済み: dpb 2016 年 10 月 11 日
Hello,
I have a matrix A containing 1000 values. I want to calculate the cumsun of the element 1 to 10, 10 to 20, 20 to 30 etc.I dont want to use a loop (for or when). Only matrix maipluation formulas.
Can you advise how I could do that?

回答 (2 件)

dpb
dpb 2016 年 10 月 10 日
編集済み: dpb 2016 年 10 月 11 日
doc arrayfun % and hide the looping construct (but it's still there)
ADDENDUM
Actually, "what was I thinking?" :) Besides the wrong function name, while can do it that way, "the Matlab way" is
windowSize = 1000;
cumsum=conv(data,ones(1,windowSizend),'valid');
The sum is nothing but the mean w/o being normalized by the number of elements in the window.
  2 件のコメント
Mato Lab
Mato Lab 2016 年 10 月 10 日
No sure I see how I can use Arrayfun to do that...
dpb
dpb 2016 年 10 月 11 日
Sorry; mistype--meant accumarray

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


Kwin
Kwin 2016 年 10 月 11 日
You can reshape the matrix such that it MATLAB only calculates the cumsum of the appropriate values:
B = cumsum(reshape(A, 10, length(A)/10))';
But if A is not a single row, you might have to do some addition reshaping to ensure that each sequence of data is in its own column (and maybe add some zeros is to make sure that the row length you are splitting up is a multiple of 10.
  1 件のコメント
dpb
dpb 2016 年 10 月 11 日
B = cumsum(reshape(A, 10, length(A)/10))';
This is good place for the empty braces idiom...
B=cumsum(reshape(A, 10, []).';

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by