Average of evey nth row of a large matrix
3 ビュー (過去 30 日間)
古いコメントを表示
This is my problem:
I have a matrix, let's say A(5000,10). So, row 1 of matrix A is comparable to row 101, row 2 is comparable to row 102 and so on. Each 100 of those sets are a bin of data (radial distribution function to be specific).
Now, I need to match up these rows and calculate their average value. So I will have a matrix of 50x10. Those 50 is the average of all the nth element (1, 101, 201 and so on). Basically I will need the average of all the bins.
0 件のコメント
採用された回答
Dave B
2021 年 8 月 11 日
To average every nth row:
a=rand(100,10);
n = 10;
mean(a(1:n:height(a),:),2)
But I think you want the average of the first block of n rows, the second block of n rows, etc. An easy way to do this is by making a little index of which rows should go into the average and then using groupsummary. This has a bonus that it's really extensible - when you later want the std of each block, it's trivially easy:
ind=floor(((1:height(a)) - 1)/n)+1 % an index of n ones, n twos, etc.
b=groupsummary(a,ind','mean')
% check that it's correct:
isequal(mean(a(1:10,:)),b(1,:))
isequal(mean(a(11:20,:)),b(2,:))
4 件のコメント
Dave B
2021 年 8 月 12 日
I'm not sure if I read that correctly, but I would expect the last line to be:
X = sum(b,3);
You want to some across the third dimension.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!