closest index in matrix between two values
이전 댓글 표시
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개)
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
2016년 7월 8일
z = bwdist(X == 2).*(X == 0);
[ii,jj] = find(z == min(z(z~=0)));
댓글 수: 2
Stephen23
2016년 7월 8일
Note: requires the Image Processing Toolbox.
Andrei Bobrov
2016년 7월 8일
Hi Stephen! MATLAB without Image... whot is it? :)
카테고리
도움말 센터 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!