Find closest value in array
조회 수: 2,425 (최근 30일)
이전 댓글 표시
I have two vector(which are time stamps) like, V N 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. Thanks
댓글 수: 0
채택된 답변
Joe S
2024년 11월 14일 0:00
편집: MathWorks Support Team
2024년 11월 14일 5:59
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.
댓글 수: 6
David
2023년 7월 5일
This just saved my night!
For anybody juse searching the index, faster variant:
[~,closestIndex] = min(abs(N-V));
추가 답변 (4개)
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
reetu hooda
2018년 2월 17일
if N is just a decimal number and it is to be searched in a matrix V(containing decimal numbers). how would the code change?
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
2014년 8월 25일
How about this:
clc;
% 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);
end
end
end
% Report to command window:
distances
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 =
0.5309
0.6544
0.4076
0.8200
0.7184
N =
0.9686
0.5313
0.3251
0.1056
0.6110
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
2017년 11월 2일
You can try this:
% Sample data
numberOfRows = 5;
V = rand(numberOfRows, 1)
N = rand(numberOfRows, 1)
% Find min distance
distances = pdist2(V, N)
[minDistance, index] = min(distances(:))
[minVRow, minNRow] = ind2sub(size(distances), index)
fprintf('The closest distance is %f which occurs between\nrow %d of V (%f) and\nrow %d of N (%f)\n',...
minDistance, minVRow, V(minVRow), minNRow, N(minNRow));
% Double-check / Prove it
V(minVRow) - N(minNRow)
John D'Errico
2024년 7월 10일
편집: John D'Errico
2024년 7월 10일
To be honest, the easiest way is to use knnsearch. It works well in one dimension, as you have here, and it should be quite efficient.
V = [1375471092848936; 1375473388165900; 1375471320476780; 1375473388947681; 1375473392527002];
N = [1375473384440853; 1375471277856598; 1375473388165900; 1375471322465961; 1375471335206288];
help knnsearch
ids = knnsearch(N,V)
There is no need to look at differences, compute absolute values, etc. Just use the tool that is designed to solve your problem directly.
댓글 수: 0
Korosh Agha Mohammad Ghasemi
2024년 6월 25일
이동: Voss
2024년 6월 25일
% Example V and N vectors
V = [1375471092848936; 1375473388165900; 1375471320476780; 1375473388947681; 1375473392527002];
N = [1375473384440853; 1375471277856598; 1375473388165900; 1375471322465961; 1375471335206288];
W = 1e4; % Window size
% Initialize the closest times array
closest_times = zeros(size(V));
% Find the closest time in N for each time in V within the window
for i = 1:length(V)
% Calculate the absolute differences
diffs = abs(N - V(i));
% Find the indices within the window
within_window = diffs <= W;
if any(within_window)
% Find the closest time
[~, closest_idx] = min(diffs(within_window));
% Get the actual index in N
closest_times(i) = N(find(within_window, closest_idx, 'first'));
else
% No times within the window
closest_times(i) = NaN;
end
end
% Display the closest times
disp('Closest times:');
disp(closest_times);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!