for loops for local matrix averaging

1 Ansicht (letzte 30 Tage)
Zack Bayhan
Zack Bayhan am 6 Dez. 2014
Kommentiert: Image Analyst am 7 Dez. 2014
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)

Antworten (2)

Image Analyst
Image Analyst am 6 Dez. 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 Kommentare
Zack Bayhan
Zack Bayhan am 6 Dez. 2014
Bearbeitet: Zack Bayhan am 7 Dez. 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.
Image Analyst
Image Analyst am 7 Dez. 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.

Melden Sie sich an, um zu kommentieren.


Zack Bayhan
Zack Bayhan am 6 Dez. 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

Community Treasure Hunt

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

Start Hunting!

Translated by