for loops for local matrix averaging
1 view (last 30 days)
Show older comments
Hi all, I'm attempting to make a code that will move into every inner box of a matrix taking the average of the cell directly above, below, left and right of it. Then move to the right one box and do the same and repeat this process until the end. I have a little of it working but I'm currently running into problems getting it to move down a line and then work right. It currently does the top row, and then the first column. Any pointers would be great, the Excel file it pulls is just a random mxn matrix.
clear all
a=xlsread('Matrix1.xlsx','sheet1');
x=0:.01:20;
for total=1:100000
for j=2:14;
for i=2:14;
hl(i,j)=a(i-1,j);
hr(i,j)=a(i+1,j);
hh(i,j)=a(i,j+1);
hb(i,j)=a(i,j-1);
ha(i,j)=.25*(hl(i,j)+hr(i,j)+hh(i,j)+hb(i,j));
h(i,j)=ha(i,j);
j=j+1;
end
end
end
contour(h,x)
0 Comments
Answers (2)
Image Analyst
on 6 Dec 2014
Use conv2():
% Define which of the 9 elements in the window will be considered for averaging.
kernel = [0, 1, 0; 1, 0, 1; 0, 1, 0]/4;
% Divided by 4 above to convert the sum into the average.
% Now use conv2() to get the local average:
output = conv2(inputMatrix2D, kernel, 'valid');
3 Comments
Image Analyst
on 7 Dec 2014
If you want to repeatedly blur/average the output, you can put the conv2 in a loop where it blurs the output over and over. Just make sure it blurs the output, not the original, each time or else the result with not change iteration after iteration, as it sounds like you found out.
Sorry, but I didn't really look at the algorithm on sheet 3 so I don't know if that's what you want to do or not.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!