% REFSORT sorts a vector of complex numbers according to their distance
% to the elements in a reference vector of the same length.
%
% zo = REFSORT(zref, zi)

function zo = refsort(zref, zi)
zo = zi;

for ii = 1:length(zi)
  [s ind] = min(abs(zref(ii)-zi));
  zo(ii) = zi(ind);
  zi(ind) = Inf;
end
