for loops for local matrix averaging
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (2개)
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
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!