Add searchsortednext and searchsortedprevious #4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adding
searchsortednext
andsearchsortedprevious
Two common tasks involving finding the nearest point, but not the same: finding the "nearest but greater than point", ie next point, or "nearest but smaller than point", ie previous point. I added two methods for those.
We often use that sort of functionality in animal behavior data when we want a label corresponding to the beginning or the end of a trial.
It's true that one could type
searchsortedfirst(search_item .<= sorted_list, true)
to find the nearest next point orsearchsortedfirst(search_item .<= sorted_list, true) -1
to find the previous. But encountering lines like those in someone else's code can be opaque. Better to have a function that more or less documents the action taken. Hence why it's nice to have these.It's also worth mentioning that there's some precedence for these modes in other languages. Matlab's
interp1
has method=nearest
setting, but alsonext
andprevious
. (see https://www.mathworks.com/help/matlab/ref/interp1.html#btwp6lt-1-method)