Average of every 25 rows in a table

4 vues (au cours des 30 derniers jours)
Reem RA
Reem RA le 3 Juil 2022
Commenté : Reem RA le 5 Juil 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

Réponse acceptée

Abhishek Tiwari
Abhishek Tiwari le 3 Juil 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 commentaires
Abhishek Tiwari
Abhishek Tiwari le 5 Juil 2022
Modifié(e) : Abhishek Tiwari le 5 Juil 2022
Just defining dimension of output table... It is better than create one dynamically, saves time. When we average every 25 rows for each column dimension of output table is reduced from 1000 to 1000/25 (=40) but column remains same.
Reem RA
Reem RA le 5 Juil 2022
Thank you, I tried it and it worked.

Connectez-vous pour commenter.

Plus de réponses (1)

Akira Agata
Akira Agata le 3 Juil 2022
Modifié(e) : Akira Agata le 3 Juil 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 commentaires
Akira Agata
Akira Agata le 4 Juil 2022
Hi @Rulla RA-san,
I just wanted to create "group number".
For more information, please take a look at the following.
Reem RA
Reem RA le 5 Juil 2022
Thanks a lot, this worked well too!!!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by