closest index in matrix between two values

3 ビュー (過去 30 日間)
df df
df df 2016 年 7 月 8 日
コメント済み: Andrei Bobrov 2016 年 7 月 8 日
hello, i have 2d matrix for example:
X = [0 0 0 0 0;
1 1 1 1 1;
0 1 2 1 0;
1 1 1 1 1;
0 0 0 0 0]
i want to get closest index of columns and rows between value 2 and 0. Is there any chance to do this??
Regards
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 7 月 8 日
are you looking for two values?
df df
df df 2016 年 7 月 8 日
i want index of value 0 which is closest to index of value 2 which means i want to get index (3,1) (1,3) (5,3) and (3,5) bcs they are only 2 cells away

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

採用された回答

Stephen23
Stephen23 2016 年 7 月 8 日
編集済み: Stephen23 2016 年 7 月 8 日
X = [0 0 0 0 0;
1 1 1 1 1;
0 1 2 1 0;
1 1 1 1 1;
0 0 0 0 0];
[R0,C0] = find(X==0);
[R2,C2] = find(X==2);
Rm = bsxfun(@minus,R0,R2.');
Cm = bsxfun(@minus,C0,C2.');
% Xd = abs(Rm) + abs(Cm);
Xd = sqrt(Rm.^2 + Cm.^2); % this could probably be improved.
[R,~] = find(Xd==min(Xd(:))); % and a tolerance might be useful here...
and the indices of the zeros closest to the two are:
>> [R0(R),C0(R)]
ans =
3 1
1 3
5 3
3 5
  1 件のコメント
df df
df df 2016 年 7 月 8 日
thanks a lot!!

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

その他の回答 (2 件)

José-Luis
José-Luis 2016 年 7 月 8 日
X = [0 0 0 0 0;
1 1 1 1 1;
0 1 2 1 0;
1 1 1 1 1;
0 0 0 0 0];
[tx,ty] = find(X==2);
x_dist = abs(tx-(1:size(X,1)));
y_dist = abs(ty-(1:size(X,2)))';
dist_array = bsxfun(@plus,x_dist.^2,y_dist.^2);
[ix,iy] = find(dist_array == min(dist_array(X==0)))

Andrei Bobrov
Andrei Bobrov 2016 年 7 月 8 日
z = bwdist(X == 2).*(X == 0);
[ii,jj] = find(z == min(z(z~=0)));
  2 件のコメント
Stephen23
Stephen23 2016 年 7 月 8 日
Note: requires the Image Processing Toolbox.
Andrei Bobrov
Andrei Bobrov 2016 年 7 月 8 日
Hi Stephen! MATLAB without Image... whot is it? :)

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


Translated by