qfind.m 766 B

12345678910111213141516171819202122232425262728293031323334353637
  1. function ys = qfind(x,ts)
  2. % function y= stats.qfind(x,ts)
  3. % x is a vector , t is the target (can be one or many targets),
  4. % y is same length as ts
  5. % does a binary search: assumes that x is sorted low to high and unique.
  6. ys=zeros(size(ts));
  7. for i=1:length(ts)
  8. t=ts(i);
  9. if isnan(t)
  10. y=nan;
  11. else
  12. high = length(x);
  13. low = 0;
  14. if t>=x(end)
  15. y=length(x);
  16. else
  17. try
  18. while (high - low > 1)
  19. probe = ceil((high + low) / 2);
  20. if (x(probe) > t)
  21. high = probe;
  22. else
  23. low = probe;
  24. end
  25. end
  26. y=low;
  27. catch
  28. y=low;
  29. end
  30. end
  31. end
  32. ys(i)=y;
  33. end