for loops for local matrix averaging

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)

Answers (2)

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

Regarding your "Answer" below, the "inside" of your matrix is all 1's. So the top, bottom, left, and right value are all 1. When you add those you get 4 and then you divide by 4 you get 1. So the average of a bunch of 1's is 1 and there's no change.
What would you like the average to be? Do you have a better value?
Zack Bayhan
Zack Bayhan on 6 Dec 2014
Edited: Zack Bayhan on 7 Dec 2014
Sorry for the vague description still trying to work it all out, this is supposed to be iterative procedure throughout the matrix modeling a flow of a fluid through a pipe. I've accomplished it in excel on sheet 3 of the attached file however I can't figure out the loop logic to accomplish the same task. I attempted to toss a loop around the above mentioned code and have yet to have any success getting the calculations to carry through the matrix. So what is supposed to happen is when one value is changed the rest of the values for the matrix should adjust to the new averaged values.
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.

Sign in to comment.

Zack Bayhan
Zack Bayhan on 6 Dec 2014
I may be missing something here, the code above seems to take the average over the edges of the matrix but doesn't change the interior values of said matrix. This happens with or without the loops. So when you look at the original matrix which is in excel labeled as a and the output matrix the value for the ones doesn't change in the middle of the matrix? how would I have it take a local average for each cell and find the point of convergence? Thanks for the help Zack

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 6 Dec 2014

Commented:

on 7 Dec 2014

Community Treasure Hunt

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

Start Hunting!