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