Average of every 25 rows in a table

5 Ansichten (letzte 30 Tage)
Reem RA
Reem RA am 3 Jul. 2022
Kommentiert: Reem RA am 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

Akzeptierte Antwort

Abhishek Tiwari
Abhishek Tiwari am 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 Kommentare
Abhishek Tiwari
Abhishek Tiwari am 5 Jul. 2022
Bearbeitet: Abhishek Tiwari am 5 Jul. 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 am 5 Jul. 2022
Thank you, I tried it and it worked.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Akira Agata
Akira Agata am 3 Jul. 2022
Bearbeitet: Akira Agata am 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 Kommentare
Akira Agata
Akira Agata am 4 Jul. 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 am 5 Jul. 2022
Thanks a lot, this worked well too!!!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by