from
find_closest_row
by Justin Winokur
Find the index of a row within a larger array while allowing for a tolerance
|
| found_index=find_closest_row(full_array,row,tol) |
function found_index=find_closest_row(full_array,row,tol)
%% find_closest_row -- Find the index of a row in a larger array
%
% Usage:
% found_index=find_closest_row(full_array,row)
% found_index=find_closest_row(full_array,row,tolerance)
% Inputs:
% full_array - The array you are searching
% row - The row of interest
% tolerance - (optional) The tolerance for the closest row (defaults to
% zero)
% Output:
% found_index - The index of the closest row. Outputs -1 if not found. If
% two or more rows are equally distant, it returns the
% first one and outputs a message to that affect.
% Description:
% Finds the closest row within the tolerance (using the Euclidean
% distance). If there is more than one row equally distanct, it
% defaults to the first one found. If there are more than two
% within tolerance but NOT equally distant, it returns the closer
% one. Machine percision is taken into account.
%
% See Also
% ismember, find
%
% Copyright, Justin Winokur 2013
if(nargin==2)
tol=0;
end
n=size(full_array,1);
distance=sqrt(sum((full_array-row(ones(n,1),:)).^2,2)); %Euclidean Distance
if((min(distance)-tol)<=eps) %found within machine precision (eps)
found_index=find((distance-min(distance))<=eps); % Finds the closest
% take first if more than one
if(length(found_index)~=1)
disp('Two or more are equal distance. Chose the first one')
end
found_index=found_index(1);
else
found_index=-1;
end
|
|
Contact us