Average of every 25 rows in a table

3 views (last 30 days)
Rulla RA
Rulla RA on 3 Jul 2022
Commented: Rulla RA on 5 Jul 2022
Hello,
I have a table of 10 columns and 1000 rows. I would like to take the average of each column for every 25 rows. How to code that please?
Thank you

Accepted Answer

Abhishek Tiwari
Abhishek Tiwari on 3 Jul 2022
Hi,
This might work,
% Sample data
data = rand(1000, 10);
% Calculating Output Dimension (#columns will be same)
rows = 1000/25;
output = ones(rows, 10);
% Start first 25 entries then compute mean and increase index for next
% 25 entries
for row = 0:rows-1
output(row+1, :) = mean(data(25*row+1:25*(row+1),:));
end
output
output = 40×10
0.5329 0.5851 0.5010 0.5203 0.4864 0.3876 0.5029 0.5378 0.4667 0.4931 0.5315 0.5217 0.5169 0.5313 0.5383 0.4457 0.4606 0.5541 0.4590 0.6092 0.4712 0.4571 0.4326 0.4300 0.5599 0.5561 0.5522 0.5662 0.5468 0.5205 0.4788 0.4865 0.4554 0.4486 0.5526 0.5052 0.3405 0.5116 0.5173 0.5189 0.4536 0.3814 0.5256 0.4594 0.4725 0.5219 0.5299 0.4808 0.4840 0.5354 0.3794 0.3971 0.4839 0.5850 0.3706 0.5975 0.6139 0.5391 0.3941 0.5435 0.5360 0.5580 0.5243 0.5750 0.4471 0.4273 0.5424 0.4981 0.4707 0.4634 0.4814 0.4900 0.6158 0.4795 0.4305 0.4495 0.4998 0.5014 0.4877 0.4645 0.4974 0.4583 0.4654 0.6242 0.4960 0.5045 0.4318 0.4466 0.3654 0.5716 0.4906 0.4275 0.3932 0.5329 0.5218 0.5332 0.4855 0.4368 0.3938 0.4639
  3 Comments
Rulla RA
Rulla RA on 5 Jul 2022
Thank you, I tried it and it worked.

Sign in to comment.

More Answers (1)

Akira Agata
Akira Agata on 3 Jul 2022
Edited: Akira Agata on 3 Jul 2022
Another possible solution:
% Sample data
data = rand(1000, 10);
% Grouping
group = repelem(1:size(data, 1)/25, 25)';
% Apply mean function for each group
output = splitapply(@mean, data, group);
  3 Comments
Rulla RA
Rulla RA on 5 Jul 2022
Thanks a lot, this worked well too!!!

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!