MATLAB: Finding distance within a range of distance

distance

[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

Best Answer

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