function [index,distance]=near(x,x0,n);
% NEAR  finds the indices of x that are closest to the point x0.
%function [index,distance]=near(x,x0,[n]);
%     x is an array, x0 is a point, n is the number of closest points to get
%     (in order of increasing distance).  Distance is the abs(x-x0)
% rsignell@usgs.gov

if nargin==2
     n=1;
end
[distance,index]=sort(abs(x-x0));
distance=distance(1:n);
index=index(1:n);

end
nctoolbox
github-repos/MATLAB/nctoolbox-nctoolbox/nctoolbox-af757acccfcac373e35fde89fc8ed7e64b67de82/cdm/utilities/misc/near.m
nctoolbox
github-repos/MATLAB/nctoolbox-nctoolbox
af757acccfcac373e35fde89fc8ed7e64b67de82