How to average 5 rows of a matrix iteratively?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(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);
0 个评论
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 个评论
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 Center 和 File Exchange 中查找有关 Numeric Types 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!