How to average 5 rows of a matrix iteratively?

조회 수: 1 (최근 30일)
Samuel White
Samuel White 2018년 9월 5일
댓글: Star Strider 2018년 9월 5일
I have a large matrix. I wish to avaerage the data from 5 rows and write the values to a new matrix iteratively until the whole matrix has been averaged. How can I do this? Many thanks.
  댓글 수: 4
Star Strider
Star Strider 2018년 9월 5일
The matrix you posted has 44 rows and 10 columns.
What result do you want?
Samuel White
Samuel White 2018년 9월 5일
Well, it actually has 5000 rows and 10 collumns. I want to average sets of 5 rows ideally :)

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

답변 (2개)

Joel Miller
Joel Miller 2018년 9월 5일
I am unsure whether you want to average rows 1-5, 2-6, 3-7, etc., or rows 1-5, 6-10, 11-15, etc. I assume the number of rows in your array, y, is an integer multiple of 5. You can create a 3D array to get a solution for the former case:
for k=1:5
z(:,:,k)=circshift(y,1-k,1);
end
z=z(1:end-4,:,:);
m=mean(z,3);
Likewise, for the latter case, create a 3D array:
for k=1:size(y,2)/5
z(k,:,:)=permute(y((k-1)*5+1:(k-1)*5+5,:),[3 2 1]);
end
m=mean(z,3);

Star Strider
Star Strider 2018년 9월 5일
This creates ‘blocks’ of 5 rows each, then takes the mean down the columns (across rows) of each ‘block’. I shortened the matrix you posted by 4 rows (to make the number of rows an integer multiple of 5), so I am re-posting it here as well as the code.
M = [0 0.1904 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0098 0 6.5535
0 0.1904 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0100 0 6.5535
0 0.1904 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0100 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0098 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0100 0.0002 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0099 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0098 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0099 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0099 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0099 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0099 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0099 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0099 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0099 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0099 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0099 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0099 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0100 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0098 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0099 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0099 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0100 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0098 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0100 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0098 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0100 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0100 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0099 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0100 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0098 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0099 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0100 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0100 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0099 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0098 0 6.5535
0 0.1903 -0.0235 0.0690 0.0106 -0.0001 -0.0179 0.0104 0 6.5535
0 0.1902 -0.0235 0.0691 0.0106 -0.0001 -0.0179 0.0095 0 6.5535
0 0.1901 -0.0235 0.0691 0.0106 -0.0001 -0.0179 0.0099 0 6.5535
0 0.1900 -0.0236 0.0692 0.0106 -0.0001 -0.0179 0.0099 0 6.5535
0 0.1899 -0.0236 0.0692 0.0106 -0.0001 -0.0179 0.0099 0 6.5535];
M3 = reshape(M', size(M,2), 5, []); % Reshape ‘M’
M3 = permute(M3, [2 1 3]); % Change Dimension Order
Mmean5 = squeeze(mean(M3,3)) % Take Row Means Of 5-Row ‘Blocks’
Experiment to get the result you want.
  댓글 수: 2
Samuel White
Samuel White 2018년 9월 5일
Thank you for your response. Unfortunately I am quite new to matlab and so I'm not sure I quite understand. I tried the code but it keeps returning an error:
Error using reshape Product of known dimensions, 45, not divisible into total number of elements, 24957.
Error in filefix (line 3) M3 = reshape(M', size(M,2), 5, []); % Reshape M
Many thanks
Star Strider
Star Strider 2018년 9월 5일
My pleasure.
My code returns the mean values of ‘blocks’ of 5 rows, and I shortened your matrix to 40 rows (an integer multiple of 5) so that it would be compatible with 5-row blocks.
The matrix you are working with has 2 incompatible rows:
ExtraRows = rem(24957, 5)
ExtraRows =
2
Probably the easiest way to deal with that is:
M3 = reshape(M(1:24955,:)', size(M,2), 5, []); % Reshape ‘M’
M3 = permute(M3, [2 1 3]); % Change Dimension Order
Mmean5 = squeeze(mean(M3,3)) % Take Row Means Of 5-Row ‘Blocks’
then:
Last2 = mean(M(end-1:end,:))
You can then append (concatenate) ‘Last2’ to the end of ‘Mmean5’ as:
Mmean5 = [Mmean5; Last2];
or just keep them separate.
This last is UNTESTED CODE, since I do not have your matrix. It should work.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by