How do I get the average of every n rows for every column in a matrix?

6 ビュー (過去 30 日間)
amoda
amoda 2022 年 8 月 30 日
編集済み: Matt J 2022 年 8 月 30 日
Hi everyone,
I have a matrix Mat1 1085x1376, which I need to find the average of every n rows for every column in the matrix. I did found a solution but for a vector, I dont't how to apply it on the matrix.
Example:
Mat1 = [1 2 3; 4 5 6; 7 8 9; 10 11 12] % 4x3. Required is the average of every n=2 rows for Mat1.
Mat1 = 4×3
1 2 3 4 5 6 7 8 9 10 11 12
% which should be a Matrix
SOL = [2.5 3.5 4.5; 8.5 9.5 10.5] % 2x3
SOL = 2×3
2.5000 3.5000 4.5000 8.5000 9.5000 10.5000
For a vector I used to use following code
n = 335;
Disp1 = nanmean(reshape( [Vector1(:);nan(mod(-numel(Vector1),n),1)],n,[]))
I would be grateful for any help!

採用された回答

David Hill
David Hill 2022 年 8 月 30 日
a=randi(100,15,10);
b=[];
n=5;
for k=1:n:size(a,1)-n
b=[b;mean(a(k:k+n-1,:))];
end
  1 件のコメント
amoda
amoda 2022 年 8 月 30 日
Hi David,
thanks, it works beyond that if the size of my matrix dividable by the chosen n or not.

サインインしてコメントする。

その他の回答 (2 件)

Matt J
Matt J 2022 年 8 月 30 日
編集済み: Matt J 2022 年 8 月 30 日
Mat1= [1 2 3; 4 5 6 ; 7 8 9; 10 11 12],
Mat1 = 4×3
1 2 3 4 5 6 7 8 9 10 11 12
[m,n]=size(Mat1);
rows=2;
SOL=reshape(Mat1, rows,1,[]);
SOL=mean(SOL,1,'omitnan');
SOL=reshape(SOL,[],n)
SOL = 2×3
2.5000 3.5000 4.5000 8.5000 9.5000 10.5000
  3 件のコメント
amoda
amoda 2022 年 8 月 30 日
Hello Matt,
thanks a lot your function is quite great for beginners just like me, but it has only one disadvantage, the size of my matrix should be dividable by the chosen n, which make the options a bit limited.
Matt J
Matt J 2022 年 8 月 30 日
編集済み: Matt J 2022 年 8 月 30 日
Easy enough to pre-pad:
Mat1= [1 2 3; 4 5 6 ; 7 8 9; 10 11 12];
[m,n]=size(Mat1);
rows=3; %bin 3 rows
mc=ceil(m/rows)*rows; %pre-padding
Mat1(m+1:mc,:)=nan
Mat1 = 6×3
1 2 3 4 5 6 7 8 9 10 11 12 NaN NaN NaN NaN NaN NaN
SOL=reshape(Mat1, rows,1,[]);
SOL=mean(SOL,1,'omitnan');
SOL=reshape(SOL,[],n)
SOL = 2×3
4 5 6 10 11 12

サインインしてコメントする。


Image Analyst
Image Analyst 2022 年 8 月 30 日
You can do this in only 2 lines of code (not including setup and comments) if you have the Image Processing Toolbox with the blockproc function, which is specifically built for this kind of operation:
%===============================================================================================================================
% Setup: Define input matrix
Mat1 = [1 2 3; 4 5 6; 7 8 9; 10 11 12] % 4x3. Required is the average of every n=2 rows for Mat1.
Mat1 = 4×3
1 2 3 4 5 6 7 8 9 10 11 12
% which should be a Matrix
% SOL = [2.5 3.5 4.5; 8.5 9.5 10.5] % 2x3
%===============================================================================================================================
% Define mean function
meanFilterFunction = @(theBlockStructure) mean(theBlockStructure.data(:));
%===============================================================================================================================
% Take the mean of 2 element by 1 element block.
% Output matrix is a matrix, half as tall as the input matrix, where every
% 2 by 1 block input block is a single value in the output matrix
% that is the mean of the elements in the block.
SOL = blockproc(Mat1, [2, 1], meanFilterFunction)
SOL = 2×3
2.5000 3.5000 4.5000 8.5000 9.5000 10.5000
  1 件のコメント
amoda
amoda 2022 年 8 月 30 日
Hi Image Analyst,
thank you for your respond, unfortunately I dont have it,

サインインしてコメントする。

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by