필터 지우기
필터 지우기

finding neighbor value

조회 수: 1 (최근 30일)
Mohammad Golam Kibria
Mohammad Golam Kibria 2011년 5월 12일
Hi,
suppose I have a large matrix A of size 200X200
I need to know the neighbor value and also the neighbor position of distance 3 of the position (50,50)
can it be done easily
A =
1 2 3
3 3 6
4 6 8
4 7 7
here (2,2) value is 3. its one distance neighbor is 1,2,3,3,6,4,6,8 and position is (1,1),(1,2),(1,3),(2,1),(2,3), (3,1),(3,2),(3,3)

채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 5월 12일
variant
A = randi(170,200);
sz = size(A);
center = [50 50];
N = 3;
Outnum = A(max(abs(repmat(1:sz(2),size(A,1),1) - center(1)) ,...
abs(repmat((1:sz(1))',1,size(A,2)) - center(2))) == fix(N/2));
more with bwdist from Image Processing Toolbox
A = randi(170,200);
sz = size(A);
center = [50 50];
N = 3;
AA = zeros(sz);
AA(center(1),center(2)) = 1;
Outnum = A(bwdist(AA,'chessboard')==fix(N/2));
Hi Oleg! Variant for N > 3
A = randi(170,17,13);
sz = size(A);
center = [8 7];
N = 5;
ons = ones(N);
ons(2:end-1,2:end-1) = 0;
ij = bsxfun(@plus, center.'-fix(N/2),0:N-1);
Outnum = A(ij(1,:),ij(2,:)).*ons;
Outnum = Outnum(Outnum>0);
  댓글 수: 2
Oleg Komarov
Oleg Komarov 2011년 5월 12일
Too complicated IMHO.
Sean de Wolski
Sean de Wolski 2011년 5월 12일
I'd roll with bwdist or A(imdilate(A == 3,ones(3)))

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

추가 답변 (1개)

Oleg Komarov
Oleg Komarov 2011년 5월 12일
EDITED
To make it flexible n = distance from center
A = randi(170,17,13);
center = [8 7];
n = 2;
% Boundary check
if all(pos - n) && all(pos + n <= size(A))
B = A(center(1)-2:center(1)+2, center(2)-n:center(2)+n).';
B = B([1:2*n*(n+1) 2*(n^2 + n + 1):end]);
end

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by