matlabvectorindexinglower-boundupperbound

Lower and upper index in a sorted vector


I have a vector with timestamps as following:

1521753105.031429052352905
1521753105.081429004669189
1521753105.131428956985474
1521753105.181428909301758
1521753105.231429100036621
1521753105.281429052352905
1521753105.331429004669189
1521753105.381428956985474
1521753105.431428909301758
1521753105.481429100036621
1521753105.531429052352905
1521753105.581429004669189

The current time is:

1521753105.231428861618042

which is in between the 4th and 5th values.

The Values lies between two values

How to determine the lower and upper index to the current time?


Solution

  • One liner trick!

    A=%[your numbers]
    v=%the value to find its position
    
    braket=find(sort([A,v])==v)+[-1 0]
    

    Assumes that A was sorted originally, of course