Average of submatrices in a larger matrix

Hello,
I have a matrix M=12x15 elements. What I would like to do is to calculate the average of every submatrix of a size of 3x3 and assign that average to a 9x12 matrix. Then move on to the next submatrix and do the routine all over again.
For example if M = [1 2 3 4; 5 6 7 8; 9 10 11 12], starting at M(1,1) as the top left corner of a 3x3 of elements, take the average of that 3x3 and assign that average to the A = 9x12 matrix at A(1,1).
I've started by using a nested for loop.
for n = 1:9
for m = 3:15
average(n,m) = M(n,m)+M(n+1,m)+M(n+2,m)+M(n,m+1)+M(n+1,m+1)+M(n+2,m+1)+M(n,m+2)+M(n+1,m+2)+M(n+2,m+2)
end
end

1 Comment

Why would the output be 9 x 12 ? If you are using overlapping 3 x 3 windows then you should get out 10 x 13. If you are using non-overlapping 3 x 3 windows then you should be getting out 4 x 5.
Your current code is doing non-overlapping processing and producing 9 x 15 and is missing out on processing any element in row # 12 (your max n is 9 and you address up to row n+2 which is 11 not 12.)

Sign in to comment.

Answers (0)

Products

Tags

Asked:

on 27 Apr 2019

Commented:

on 27 Apr 2019

Community Treasure Hunt

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

Start Hunting!