필터 지우기
필터 지우기

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

조회 수: 34 (최근 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
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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by