Finding elements of one vector that are closest to elements of another

35 ビュー (過去 30 日間)
Erik J
Erik J 2020 年 11 月 5 日
コメント済み: Erik J 2020 年 11 月 5 日
I have two vectors, x and y. Both vectors contain numbers from 0 to 1. Vector x is 964 elements. Vector y is 51 elements.
I need to find the values from vector x that are closest to the values of vector y, without replacement. So if x(1) is closest to y(1), it returns x(1) - but when it moves to find the closest element to x(2), it cannot return x(1) again and must return a different value, even if x(1) was closer to y(2) than the value it returns. Basically, I need to find the 51 unique values in x that most closely align with the values in y.
Any ideas?
Thank you!

採用された回答

Matt J
Matt J 2020 年 11 月 5 日
編集済み: Matt J 2020 年 11 月 5 日
D=abs(y(:)-x(:).');
result=sortrows( matchpairs(D,10*max(D(:))) ,1)
  3 件のコメント
Matt J
Matt J 2020 年 11 月 5 日
Yep. Thanks!
Erik J
Erik J 2020 年 11 月 5 日
Wow that's slick. Works perfectly. Thank you!

サインインしてコメントする。

その他の回答 (1 件)

Adam Danz
Adam Danz 2020 年 11 月 5 日
編集済み: Adam Danz 2020 年 11 月 5 日
Here's a lower-level approach (~10x faster than matchpairs but that function is also quite fast).
  1. Use implicit expansion to create a matix of y-x values.
  2. Replace the paired x-minimum with NaN so it cannot be chosen again
  3. Create table to show results.
Demo
% Create x,y values
rng('default') % for reproducibility
x = rand(1,964);
y = rand(1,51);
% Loop through each y-value to find x-min pair
minIdx = nan(numel(y),1);
xCopy = x;
for i = 1:numel(y)
diffs = abs(y(:)-xCopy(:).');
[~,minIdx(i)] = min(diffs(i,:));
xCopy(minIdx(i)) = NaN;
end
% Confirm at all values are unique
assert(numel(unique(minIdx))==numel(minIdx), 'Index values are not unique.')
% Show pairs
T = table(y(:), x(minIdx)', y(:)-x(minIdx)', minIdx(:), ...
'VariableNames', {'y','xMin','y-x','xMinIndex'})
T = 51x4 table
y xMin y-x xMinIndex ________ ________ ___________ _________ 0.32583 0.32515 0.00068795 418 0.54645 0.54659 -0.00014367 478 0.39888 0.39859 0.00029126 690 0.41509 0.41452 0.00057085 716 0.18074 0.18185 -0.0011093 133 0.25539 0.2551 0.00029163 62 0.020536 0.019578 0.00095815 556 0.92368 0.92338 0.00029597 222 0.6537 0.65369 9.755e-06 935 0.93261 0.93285 -0.00024 527 0.16351 0.16357 -5.7541e-05 951 0.9211 0.92033 0.00076522 342 0.79466 0.79483 -0.00017353 200 0.57739 0.57672 0.00067268 290 0.44004 0.44009 -4.9543e-05 427 0.25761 0.25751 0.00010548 70
The table shows the original y-values, the paired x-minimum values, the difference between y-x (absolute), and the index of x that paired with y.
Plot the results comparing y to the paird x values and use color to indicate the difference between y and x.
figure()
scatter(T.xMin, T.y, 60, T.('y-x'), 'LineWidth', 1.5)
xlabel('x-pair')
ylabel('y')
colormap('jet')
cb = colorbar();
ylabel(cb, '|y-x|')
axis equal
grid on

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by