function j=locate(xx,x)
% USAGE: function j=locate(xx,x)
%
% Givern an array XX, and given a value x, returns J such that x is between xx(j)
% and xx(j+1), xx must be monotic, either increasing or decresing order.
% J=0 is returned to indicate that x is out of range. 
% The code is based in numerical recipes 
%
%  Pedro Velez (UIB), July 1999

n=size(xx,1);
if n(1) == 1
xx=xx';
n=size(xx,1);
end



jl=0;
ju=n+1;
while (ju-jl) > 1
  jm=round((ju+jl)/2);
  if ((xx(n) > xx(1)) & (x > xx(jm))) |(~((xx(n) > xx(1))|(x > xx(jm))))
    jl=jm;
  else
    ju=jm;
  end
end
j=jl;
