finding distance within a range of distance

조회 수: 1 (최근 30일)
Mohammad Golam Kibria
Mohammad Golam Kibria 2011년 5월 23일
[EDIT: 20110524 01:36 CDT - reformat, clarify - WDR]
Hi 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 need to know whether distances of value 5 with another 5 are in the range 3. Thanks in advance

채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 5월 24일
ij5 = find(I==5);
[i5 j5] = ind2sub(size(I),ij5);
out1 = [nchoosek(ij5,2) bsxfun(@(x,y)sqrt(diff(nchoosek(x,2),[],2).^2....
+diff(nchoosek(y,2),[],2).^2),i5,j5)];
[non,indout]=min(out1(:,3));
out = out1(indout,:);
in vector "out" the first Two members - the indices of matrix "I", the third distance between them
EDIT
out = out1(out1(:,3) <= 3,:)
EDIT EDIT
The answer to the last Mohammad comment:
[ii,jj]=ind2sub(size(I),nchoosek(find(I==5),2));
out1 = [ii jj sqrt(sum(reshape(diff([ii;jj],[],2).^2,[],2),2))];
[non,indout]=min(out1(:,5)); % 1 variant
out = out1(indout,:);
out = out1(out1(:,5) <= 3,:); % 2 variant
  댓글 수: 1
Mohammad Golam Kibria
Mohammad Golam Kibria 2011년 5월 25일
your answer is correct.but I need indices in 2D form i.e. in the form as follows:
1 3 4 3 3 the first two is the row and column of first 5
second two is the row and column of second 5 and last one is the distance. or any other convenient way to get the indices in 2D form with distance

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

추가 답변 (1개)

Wolfgang Schwanghart
Wolfgang Schwanghart 2011년 5월 23일
How about
[X,Y] = meshgrid(1:size(I,2),1:size(I,1));
ix = find(I == 5);
d = hypot(bsxfun(@minus,X(ix),X(ix)'),bsxfun(@minus,Y(ix),Y(ix)'));
[ixd,cc] = find(d<=3 & d>0);
II = false(size(I));
II(ix(ixd)) = true;
Cheers, W.
  댓글 수: 2
Mohammad Golam Kibria
Mohammad Golam Kibria 2011년 5월 24일
Your output is not clear to me.
Walter Roberson
Walter Roberson 2011년 5월 24일
Wolfgang's output matrix II has a 1 in each position at which there was originally a 5 that was within a distance of 3 of another 5.
If that output is not what you wanted, then you need to specify the form of the output that you did want.

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

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by