for loops for local matrix averaging

조회 수: 1 (최근 30일)
Zack Bayhan
Zack Bayhan 2014년 12월 6일
댓글: Image Analyst 2014년 12월 7일
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)

답변 (2개)

Image Analyst
Image Analyst 2014년 12월 6일
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
Zack Bayhan
Zack Bayhan 2014년 12월 6일
편집: Zack Bayhan 2014년 12월 7일
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 2014년 12월 7일
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.

댓글을 달려면 로그인하십시오.


Zack Bayhan
Zack Bayhan 2014년 12월 6일
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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by