How to find minimal distance between elements?
古いコメントを表示
I have a vector, and I would like to find the minimal distance between element values. Any element distance from any element in the set. Is it possible to do this without a for cycle?
1 件のコメント
Image Analyst
2018 年 3 月 10 日
Mr. M, you've now asked 311 questions and "Accepted" virtually none of them. Perhaps now you can "thank" the people who took their time to try to help you by Accepting their answers so that they get reputation points. That's the etiquette in this forum. Thanks in advance.
採用された回答
その他の回答 (5 件)
Jos (10584)
2018 年 3 月 9 日
Without creating a possibly large intermediate N-ny-N matrix or using a possibly slow sort
V = [1 8 6 4 2 10] ;
W = nchoose2(V) % all pairs of distinct elements
D = abs(W(:,2)-W(:,1)) % distance between pairs
[minD, ix] = min(D) % minD = 1
minPair = W(ix,:) % minPair = [1 2]
nchoose2 is a fast function to get all combinations of two elements, and can be downloaded from the Matlab File Exchange: https://uk.mathworks.com/matlabcentral/fileexchange/20144-nchoose2-x-
Image Analyst
2018 年 3 月 9 日
1 投票
If the "vector" is actually a matrix of (x,y) locations, you can use pdist2(). Let me know if that's the case and I'll give you an example.
3 件のコメント
aciara
2021 年 1 月 28 日
Could you give me an example please? I have to find minimal distance between elements of 2 different matrices. Is it possible?
Image Analyst
2021 年 1 月 29 日
numPoints = 7;
xy1 = rand(numPoints, 2);
xy2 = rand(numPoints, 2);
distances = pdist2(xy1, xy2);
% Set 0's to inf since we don't want to find the min
% distance of a point to itself, which is 0.
distances(distances==0) = inf
% Find min distance
minDistance = min(distances(:))
% Find row and column where it occurs.
[row1, row2] = find(distances == minDistance)
% Plot all points
plot(xy1(:, 1), xy1(:, 2), 'r.', 'MarkerSize', 30); % Plot set 1.
hold on;
plot(xy2(:, 1), xy2(:, 2), 'b.', 'MarkerSize', 30); % Plot set 1.
% Plot the line
x1 = xy1(row1, 1);
y1 = xy1(row1, 2);
x2 = xy2(row2, 1);
y2 = xy2(row2, 2);
plot([x1, x2], [y1, y2], 'k-', 'LineWidth', 2);
grid on;
legend('Set 1', 'Set 2', 'Closest Pair');
caption = sprintf('Min Distance = %.4f', minDistance);
title(caption, 'fontSize', 20);

Please vote for my Answer if it helped you.
aciara
2021 年 1 月 29 日
Thank you!! Very helpful
Von Duesenberg
2018 年 3 月 9 日
This will get you started:
dumVect = [1 3 5 30]';
[minVal, idxMin] = min(diff(dumVect))
If you work with more dimensions, you may want to use pdist instead of diff. And of course, I'll let you figure out how you want to handle ties.
2 件のコメント
Jan
2018 年 3 月 9 日
This is the minimal distance between neighboring elements, not between all elements.
Von Duesenberg
2018 年 3 月 9 日
Oops, you're right.
n = 10;
v = rand(1, n);
dist = abs(v - v.'); % Auto-expand since R2016b
dist(1:(n+1):end) = Inf; % Mask the zeros [EDITED]
% dist = bsxfun(@minus, v, v.') .^ 2; % For older versions
[minValue, minIndex] = min(dist(:));
4 件のコメント
Jos (10584)
2018 年 3 月 9 日
This just will give zeros ...
Jan
2018 年 3 月 9 日
@Jos: Of course 0 is the correct answer to the original question: "Any element distance from any element in the set". This includes the distance from an element to itself, which is zero. :-)
Of course, this was not meant, and I have edited the code now to solve: "Any element distance from any other element in the set".
Thanks for finding my mistake.
Mr M.
2018 年 3 月 14 日
Jan
2018 年 3 月 15 日
@Mr M.: You can simply try it.
v = rand(2,3)
v.'
It is the transpose operator. The quote without the dot before replies the conjugate complex value in addition.
Jos (10584)
2018 年 3 月 9 日
編集済み: Jos (10584)
2018 年 3 月 9 日
0 投票
By definition the minimum distance is zero because v(i)==v(i) for any element i of the vector v.
But I assume you want the minimum distance between v(i) and v(j) for all pairs (i,j) where i is unequal to j, but forgot to mention that ... :p
カテゴリ
ヘルプ センター および File Exchange で Deep Learning Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!