How to average 5 rows of a matrix iteratively?
이전 댓글 표시
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
madhan ravi
2018년 9월 5일
an example of matrix?
Samuel White
2018년 9월 5일
Star Strider
2018년 9월 5일
The matrix you posted has 44 rows and 10 columns.
What result do you want?
Samuel White
2018년 9월 5일
답변 (2개)
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
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
2018년 9월 5일
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.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!