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

findMATLABvector

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!

Best Answer

D=abs(y(:)-x(:).');
result=sortrows( matchpairs(D,10*max(D(:))) ,1)