qbetween2.m 926 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. function [y i] = qbetween2(x, start, finish)
  2. % [y i] = qbetween2(x, start, finish)
  3. % returns values (y) and indices (i)
  4. % works for sorted 1D vectors.
  5. % using find you get o(n) , by assuming that the vector is sorted you get
  6. % 2*o(log(n)). which is WAY better.
  7. if isempty(x) || (x(end)<start) || x(1)>finish || finish <=start
  8. y = [];
  9. i = [];
  10. return
  11. end
  12. i=stats.qfind(x, [start finish]);
  13. %qfind returns -1 if the target is less than min(x), since we are
  14. %getting 'between', we just take the first relevant x
  15. if i(1)==i(2)
  16. y=[];
  17. return
  18. elseif i(1)==0
  19. i(1)=1;
  20. end
  21. %this code deals with the fact that if there is no exact match, qfind
  22. %will return the index that is one lower than the target. since we
  23. %want between, we just double check the end points. every other point
  24. %will be valid.
  25. y=x(i(1):i(2));
  26. if y(1)<start
  27. y = y(2:end);
  28. i(1) = i(1) + 1;
  29. end
  30. if y(end)>finish
  31. y = y(1:end-1);
  32. i(2) = i(2) - 1;
  33. end