finding neighbor of a position

4 ビュー (過去 30 日間)
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 6 月 26 日
Hi, I have a matrix.
I =
1 0 0
2 5 0
0 0 3
0 0 0
I know the position of 5 in I is 6 linear index.
is there any easy function to have the 8 other neighbors of 5. Thanks

採用された回答

Oleg Komarov
Oleg Komarov 2011 年 6 月 26 日
EDITED: should be fine now
I =[ 1 0 0
2 5 0
0 0 3
0 0 0];
l = 8;
sz = size(I);
% row, col subs of center
[r,c] = ind2sub(sz,l); % c = ceil(l/4); r = mod(l,4)+ c*sz(1);
% Calculate 8 neighbors
neigh(1:8,1:2) = [r+[-1;0;1;-1;1;-1;0;1] c+[-1;-1;-1;0;0;1;1;1] ];
% Only those in the range
neigh = neigh(all(neigh,2) & neigh(:,1) <= sz(1) & neigh(:,2) <= sz(2),:);
% Convert to position
idx = (neigh(:,2)-1)*sz(1) + neigh(:,1);
  3 件のコメント
Oleg Komarov
Oleg Komarov 2011 年 6 月 27 日
Hopefuly now is ok. Tested initial and final position.
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 6 月 28 日
Thanks

サインインしてコメントする。

その他の回答 (3 件)

Sean de Wolski
Sean de Wolski 2011 年 6 月 27 日
idx = find(conv2(double(I==5),ones(3),'same'))
%This includes the 6, but that could easily be taken care of with setdiff.
  1 件のコメント
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 6 月 28 日
Thanks

サインインしてコメントする。


Wolfgang Schwanghart
Wolfgang Schwanghart 2011 年 6 月 26 日
  3 件のコメント
Wolfgang Schwanghart
Wolfgang Schwanghart 2011 年 6 月 27 日
I = [ 1 0 0;
2 5 0;
0 0 3;
0 0 0];
% find the neighbors of the elements where I = 5
I5 = I==5;
[ix,ixn] = ixneighbors(I,I5)
ix =
6
6
6
6
6
6
6
6
ixn =
10
7
2
5
9
1
11
3
% thus, ixn are the linear indices of the neighbors of the indices ix.
% You'll find the values associated with the neighbors by
I(ixn)
ans =
0
0
2
0
0
1
3
0
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 6 月 28 日
thanks,this also works fine for me

サインインしてコメントする。


Andrei Bobrov
Andrei Bobrov 2011 年 6 月 26 日
idl = 6;
idxs = ...
nonzeros(bsxfun(@plus,idl - [1 0 -1]',size(I,1)*[-1 0 1]).*[1 1 1;1 0 1;1 1 1])
CORRECTED 06/27/2011 10:05 MSK
idl = 6;
s = size(I);
I0 = zeros(s+2);
I0(2:end-1,2:end-1) = reshape(1:numel(I),s);
idxs = nonzeros(I0(bsxfun(@plus,find(I0==idl) - [1 0 -1]',(s(1)+2)*[-1 0 1])).*[1 1 1;1 0 1;1 1 1])
MORE variant (06/27/2011 11:12 MSK)
s = size(I);
[ii jj] = ind2sub(s,idl);
v = [-1 -1 -1;0 0 0;1 1 1];
R=ii+v;
C=jj+v';
loc = (R<=s(1) & R>=1&C<=s(2) & C>=1&[1 1 1;1 0 1;1 1 1])>0;
idxl = sub2ind(s,R(loc),C(loc));
MORE variant 2 (06/27/2011 11:35 MSK) with idea of Oleg
s = size(I);
[ii jj] = ind2sub(s,idl);
R = ii + [-1 0 1 -1 1 -1 0 1];
C = jj + [-1 -1 -1 0 0 1 1 1];
loc = (R<=s(1) & R>=1&C<=s(2) & C >= 1 )>0;
idxl = sub2ind(size(I),R(loc),C(loc));
LAST variant (06/27/2011 13:43 MSK)
I1 = zeros(size(I));
I1(idl)=1;
idx = find(bwdist(I1,'chessboard')==1)
or
idx = find(bwdist(I==5,'chessboard')==1)
  3 件のコメント
Andrei Bobrov
Andrei Bobrov 2011 年 6 月 27 日
Thanks Oleg! Corrected...
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 6 月 28 日
Thanks this also works fine for me

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMeasurements and Feature Extraction についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by