finding nearest distance of a distinct value
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi, suppose I have a matrix as follows:
I =
1 1 5 1 1 1 8
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 5 2 1 1 1
1 1 1 1 1 5 1
1 1 1 1 1 1 1
I(3,4) is 1 . I need to know the distance and position of nearest 5 of this matrix from this 1. How to do this easily?Thanks in advance.
댓글 수: 0
채택된 답변
Andrei Bobrov
2011년 5월 18일
EDIT
[Hi Hj] = find(I~=1);
d = [Hi Hj I(I~=1) sqrt(sum(bsxfun(@minus,[Hi Hj],[3 4]).^2,2))];
ds = sortrows(d,4);
d5 = ds(ds(:,3)==5,:);
out = d5(1,:)
more
[Hi Hj] = find(I==5);
d = [Hi Hj sqrt(sum(bsxfun(@minus,[Hi Hj],[3 4]).^2,2))];
[~,jj] = min(d(:,3));
out = d(jj,:)
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!