Cumulative mean over constantly increasing interval

Hey guys, any help would be much appreciated as I'm relatively new to the program!
I've got 400000 odd rows of data and what i need to do is calculate the mean over the first 10000 points then over the first 20000 then 30000 until I've used all the data points.
The code I've written calculates the mean over each of the 400, 10000 point intervals but doesnt increase the interval size by 10000 from the original each time.
Thanks in advance
intervalLength = 10000;
x = A.data(1:4000001,2);
%create index array idx = zeros(length(x),1); idx(1:intervalLength:end) = 1; idx = cumsum(idx);
avg = accumarray(idx,x,[],@mean);
out = avg(idx);
m = avg(1:401,1); n = [1:401]; plot(n,m); xlabel('interval number'); ylabel('x_average');

 採用された回答

Star Strider
Star Strider 2015 年 9 月 5 日
編集済み: Star Strider 2015 年 9 月 5 日

1 投票

I’m not sure what you’re doing. so I’ve done two options:
D = rand(4E+5, 2); % Create Data
r = [1E+4 : 1E+4 : size(D,1)];
for k1 = 1:length(r)
m1(k1,:) = mean(D(1:r(k1),:)); % Takes ‘mean’ Of [1:1E4], [1:2E4], &c.
end
DR = reshape(D, 1E+4, size(D,2), []);
m2 = squeeze(mean(DR, 1)).'; % Takes ‘mean’ Of All 40 1E+4 Row Sections Individually
I doubt accumarray is going to do what you want, at least any more efficiently that what I’ve done here (unless I’m not understanding what you want to do).

2 件のコメント

Yusuf
Yusuf 2015 年 9 月 5 日
Thanks Star Strider, first option is exactly what I was after!
Star Strider
Star Strider 2015 年 9 月 5 日
My pleasure!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by