Fastest way to find closest value in a vector

Illustration
Patrik Ek - 2023-03-18T13:19:03+00:00
Question: Fastest way to find closest value in a vector

Hi,   I have a serious issue in matlab. I have 2 vectors v1 and v2 and length(v1) = 1376872, length(v2) = 1350228. Vector v2 contains information about position in the original coordinate system. Vector v1 is coordinates from the wanted coordinate system, transformed to the same type of coordinates as v2.   I need to find the closest match between the coordinate systems for each point. So to say, I want a vector of the same length as v1, each element containing the index of the closest point in v2.   I have solved the problem by using a for loop.     ind = -1*ones(length(v1),1); for k = 1:length(v1) diff = v1(k)-v2; [~,minInd] = min(abs(diff)); if abs(real(diff(minInd)))<2.5 && abs(imag(diff(minInd)))<2.5 ind(k) = minInd; end end This is however a really slow process (will take around 10 hours). This time consumption is unacceptable for the application and thus I need to find a faster solution.     If anyone knows how to solve this please help. Also, if there are better ways of mapping values between coordinate systems (which there probably are) this would also be accepted.

Expert Answer

Profile picture of Neeta Dsouza Neeta Dsouza answered . 2025-11-20

You can significantly speed up the process of finding the closest value in a vector by using a more efficient approach, such as k-d trees or sorting-based methods. MATLAB has built-in functions like knnsearch from the Statistics and Machine Learning Toolbox that can help with this.

Here’s how you can use knnsearch to find the closest points efficiently:

matlab
% Sample vectors v1 and v2
v1 = rand(1376872, 1); % Example data
v2 = rand(1350228, 1); % Example data

% Find the closest indices using knnsearch
indices = knnsearch(v2, v1);

% Optional: Apply additional conditions
diff = v1 - v2(indices);
validIndices = abs(real(diff)) < 2.5 & abs(imag(diff)) < 2.5;
indices(~validIndices) = -1; % Mark invalid indices as -1

% Display result
disp(indices);

Explanation:

  • knnsearch: This function performs a nearest-neighbor search using a k-d tree, which is much faster than a brute-force search, especially for large datasets.

  • Indices Adjustment: After finding the closest indices, you can apply additional conditions (e.g., checking the real and imaginary parts of the difference) and mark invalid indices as -1.

This approach should be significantly faster than using a for loop, and it provides an efficient way to find the closest match between the coordinate systems for each point in v1.

If knnsearch is not available, you can consider sorting-based methods, which can also improve performance. However, using built-in functions like knnsearch is often the most efficient approach.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!