Average of evey nth row of a large matrix

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

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

Thanks for the solution. But this not exactly what I am looking for. Lets say I have a matrix of 10 by 2.
a=rand(12,2)
a =
0.5862 0.1559 % Row 1
0.6509 0.7744 % Row 2
0.8756 0.7171 % Rowl 3
0.3699 0.7807 % Row 4
0.5258 0.6832 % Row 5
0.0857 0.9956 % Row 6
0.0644 0.5571 % Row 7
0.1754 0.6236 % Row 8
0.6654 0.6843 % Row 9
0.4061 0.1972 % Row 10
0.7061 0.8972 % Row 11
0.3061 0.9720 % Row 12
Now what I wanted to do is to bin 4 rows together. So we have 3 bins like below,
Bin 1
0.5862 0.1559 % Row 1
0.6509 0.7744 % Row 2
0.8756 0.7171 % Rowl 3
0.3699 0.7807 % Row 4
Bin 2
0.5258 0.6832 % Row 5
0.0857 0.9956 % Row 6
0.0644 0.5571 % Row 7
0.1754 0.6236 % Row 8
Bin 3
0.6654 0.6843 % Row 9
0.4061 0.1972 % Row 10
0.7061 0.8972 % Row 11
0.3061 0.9720 % Row 12
Now what I wanted to do is take a average of the (Row1+ Row 5+ Row 9), (Row 2+ Row 6 + Row 10) and so on. Once I add them up I will end up with a matrix of , 4x 2.
When things are nice and rectangular, an easy solution is to use reshape. If your arrays aren't too big and performance isn't an issue, I'd do this.
I think the faster way is probably to use accumarray, but working out the indexing may be tricky.
a = rand(12,2);
b = reshape(a, 4, 2, [])
b =
b(:,:,1) = 0.3008 0.1450 0.7929 0.9822 0.9731 0.3776 0.8729 0.6471 b(:,:,2) = 0.6149 0.0543 0.9102 0.8559 0.9879 0.0591 0.4391 0.3196 b(:,:,3) = 0.0369 0.5496 0.8689 0.6003 0.4196 0.9514 0.2569 0.0747
sum(b,3)
ans = 4×2
0.9525 0.7489 2.5720 2.4383 2.3806 1.3881 1.5690 1.0414
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);
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

Asked:

on 11 Aug 2021

Commented:

on 12 Aug 2021

Community Treasure Hunt

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

Start Hunting!