Average of evey nth row of a large matrix
2 次查看(过去 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 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!