function [idx,ld] = closest(vec, range)

sz = size(range);

idx = 1;
r = (range(1,:) - vec);
ld = sum(r.*r);

for i=2:sz(1),
  r = (range(i,:) - vec);
  d = sum(r.*r);

  if (d < ld)
    ld = d;
    idx = i;
  end
end
