Find closest value in array

3,444 次查看(过去 30 天)
I have two vector(which are time stamps) like,
1375471092848936 1375473384440853
1375473388165900 1375471277856598
1375471320476780 1375473388165900
1375473388947681 1375471322465961
1375473392527002 1375471335206288
.................. ..................
My goal is to find closest time in N with respect to V (i.e. find time in N which is nearly equal with V). My frame is W = 1e4, furthermore V should lies between N-W and N+W. So how do I get closest time through MATLAB? Any help would be appreciated.


Joe S
Joe S 2018-9-10
编辑:MathWorks Support Team 2018-11-27
To compute the closest value in a vector “N” for each element of “V”, try the following code with example vectors “N” and “V”:
V = randi(10,[5 1])
N = randi(10,[5 1])
A = repmat(N,[1 length(V)])
[minValue,closestIndex] = min(abs(A-V))
closestValue = N(closestIndex)
Note that if there is a tie for the minimum value in each column, MATLAB chooses the first element in the column.
  4 个评论
Adrian Aquino Arriaga
Great answer. I would only use dot-apostrophe (.') instead of only apostrophe to make sure you are taking the non-conjugate transpose. Of course, this is only relevant if you are working with complex numbers.


更多回答(3 个)

Andrew Reibold
Andrew Reibold 2014-8-25
编辑:Andrew Reibold 2014-8-25
This finds the value in N which is closest to the V value I am calling.
N = [1990 1998 2001 2004 2001]
V = [2000 2011 2010 2001 1998]
[c index] = min(abs(N-V(1)))
In this case Im looking for the closest value to 'V(1)' which is 2000. It should return the 3rd or 5th value of N which is 2001.
Note: 'index' is the index of the closest value. If two are the same, like in this example with two different '2001's, it will return the index of the first one.
  4 个评论
Image Analyst
Image Analyst 2018-2-17
reetu, if N is just a single number then you can do this
[minDistance, indexOfMin] = min(abs(V-N));


Image Analyst
Image Analyst 2014-8-25
How about this:
% Sample data
numberOfRows = 5;
V = rand(numberOfRows, 1)
N = rand(numberOfRows, 1)
% Find min distance
minDistance = inf;
for ni = 1 : numberOfRows
for vi = 1 : numberOfRows
distances(vi, ni) = abs(N(ni) - V(vi));
if distances(vi, ni) < minDistance
minNRow = ni;
minVRow = vi;
minDistance = distances(vi, ni);
% Report to command window:
fprintf('Closest distance is %f which occurs between row %d of N and row %d of V\n',...
minDistance, minNRow, minVRow);
In the command window:
V =
N =
distances =
0.4378 0.0005 0.2057 0.4252 0.0801
0.3142 0.1231 0.3293 0.5488 0.0435
0.5610 0.1237 0.0825 0.3020 0.2033
0.1487 0.2886 0.4948 0.7144 0.2090
0.2503 0.1870 0.3932 0.6127 0.1074
Closest distance is 0.000470 which occurs between row 2 of N and row 1 of V
  3 个评论
Image Analyst
Image Analyst 2017-11-10
What's wrong with a for loop? And what is ni and vi?


Eswar Aravind Swamy Adari
I have a matrix A of size [30x36] and B of size [38x36]. How do I find the closest pairs?
FYI- Those are the HOG Descriptors of block size 16x16 and cell size 8x8. I am trying to find the closest HOG descriptors.
Thanks in advance.


Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by