GetIntervalsIndex.m 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. % function GetIntervalsIndex:
  2. %
  3. % Uses the data along with a data value and returns the intervals (as indices)
  4. % for the provided value
  5. %
  6. % input:
  7. % data - is a data vector
  8. % value - value to use for intervals calculation
  9. %
  10. % output:
  11. % intervals - nx2 array (start index, end index)
  12. function [intervals] = GetIntervalsIndex(data, value)
  13. assert(size(data,1) == 1 | size(data,2) == 1, 'Data should be a vector');
  14. % initialize data
  15. intervals = zeros(0,2);
  16. % find position of attribute in data
  17. startIndex = -1;
  18. for i=1:size(data,1)
  19. if (data(i)==value)
  20. % first element of interval
  21. if (startIndex==-1)
  22. startIndex=i;
  23. end
  24. else
  25. % interval finished on previous iteration
  26. if (startIndex~=-1)
  27. intervals = [intervals; startIndex i-1];
  28. end
  29. startIndex = -1;
  30. end
  31. end
  32. % add last interval
  33. if (startIndex~=-1)
  34. intervals = [intervals; startIndex length(data)];
  35. end
  36. end