Average of every 25 rows in a table

조회 수: 6 (최근 30일)
Reem RA
Reem RA 2022년 7월 3일
댓글: Reem RA 2022년 7월 5일
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

채택된 답변

Abhishek Tiwari
Abhishek Tiwari 2022년 7월 3일
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
Abhishek Tiwari
Abhishek Tiwari 2022년 7월 5일
편집: Abhishek Tiwari 2022년 7월 5일
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 2022년 7월 5일
Thank you, I tried it and it worked.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Akira Agata
Akira Agata 2022년 7월 3일
편집: Akira Agata 2022년 7월 3일
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
Akira Agata
Akira Agata 2022년 7월 4일
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 2022년 7월 5일
Thanks a lot, this worked well too!!!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by