Average of evey nth row of a large matrix
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Dave B
on 11 Aug 2021
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 Comments
Dave B
on 12 Aug 2021
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.
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!