Average of evey nth row of a large matrix

1 view (last 30 days)
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.

Accepted Answer

Dave B
Dave B on 11 Aug 2021
To average every nth row:
a=rand(100,10);
n = 10;
mean(a(1:n:height(a),:),2)
ans = 10×1
0.7132 0.5279 0.5908 0.5117 0.4052 0.5002 0.4536 0.5225 0.4116 0.4415
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.
ind = 1×100
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3
b=groupsummary(a,ind','mean')
b = 10×10
0.4956 0.5382 0.4825 0.5698 0.4879 0.4804 0.4992 0.7141 0.6146 0.3100 0.4091 0.6328 0.6922 0.4547 0.4437 0.4993 0.4103 0.6151 0.4009 0.5329 0.5224 0.6052 0.5603 0.2870 0.5020 0.4421 0.6260 0.5311 0.5578 0.5436 0.4771 0.3879 0.3807 0.4743 0.2958 0.3670 0.5435 0.5268 0.5284 0.4745 0.4394 0.4199 0.6134 0.3321 0.4016 0.5233 0.4660 0.5426 0.5008 0.5224 0.3314 0.7157 0.4940 0.7073 0.5422 0.4870 0.4935 0.5722 0.6656 0.4735 0.4324 0.6773 0.3981 0.5952 0.4559 0.3111 0.6122 0.3747 0.6102 0.5560 0.5078 0.5559 0.5401 0.3599 0.5674 0.4305 0.5721 0.5610 0.4347 0.4187 0.5629 0.4248 0.6734 0.3919 0.4130 0.6040 0.3570 0.6186 0.4929 0.5233 0.3555 0.3813 0.4285 0.5278 0.4954 0.5590 0.5031 0.4028 0.3891 0.3415
% check that it's correct:
isequal(mean(a(1:10,:)),b(1,:))
ans = logical
1
isequal(mean(a(11:20,:)),b(2,:))
ans = logical
1
  4 Comments
Avik Mahata
Avik Mahata on 12 Aug 2021
Thanks a lot of your response.
Well, it did worked. My arrays are big, but it can does the compute within seconds. However, the arrays are becoming 3d and I am not been able to print out the final sum matrix. I have total of 554894 rows, which will be binned into 5494 different matrices each having 101 rows. Then I just sum it and print it. It seems the command is working and quite fast. However, its creating a 3D matrix, that I am not able to print it out.
data=dlmread('RDFdata');
M = data(:,13);
b = reshape(data, 5494, 101, []);
X = sum(b,101);
Dave B
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.

Sign in to comment.

More Answers (0)

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!