In Julia, how do I get the index of the first element in a sorted array that exceeds a certain value?

Here is my code ( x- a sorted array):

lookup_value = 310.0
x = [298.0, 303.0, 308.0, 313.0, 323.0]
if (issorted(x))
    idx = searchsorted(x, lookup_value)
end

In this particular case, the idx value is:

4:3

Here I would like to extract either "4" (the first element that exceeds my search value) or "3" (the last element that does not exceed my search value). However, I cannot do this by converting the range to an array, since all I get from the following command is an empty array:

julia> collect(idx)
0-element Array{Int64,1}

Note that the search value of 310.0 is just an example; this variable can take on different values.

+4
source share
2 answers

, idx.start idx.stop .

julia> idx
4:3
julia> idx.start
4
julia> idx.stop
3

, , x[idx.start]. , , x[idx.stop].

+4

. , , , :

idx.start idx.stop. Julia () , . .

first(idx) last(idx), Fengyang Wang.

0

All Articles