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

nathanielng

Here is my code (x is the 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 value of idx is:

4:3

Here, I'd like to extract either the "4" (the first element exceeding my look up value) or "3" (the last element that does not exceed the look up value). However, I'm unable to do so by converting the range to an array, as all I get from the following command is an empty array:

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

Note that the lookup value of 310.0 is only an example; this variable could take on different values.

nathanielng

I just realised its possible to use idx.start and idx.stop to access the start and end of a range.

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

The first element that exceeds the lookup value is x[idx.start]. The last element that does not exceed the lookup value is x[idx.stop].

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I loop through an array and stop at a certain index

From Java

How do I check in JavaScript if a value exists at a certain array index?

From Java

How do I get the first element while continue streaming?

From Dev

how do i insert element in knockout array at specific index

From Dev

How do I select the first sibling of a certain element type with jQuery?

From Dev

In Angular, how can I get the first element of a filtered array?

From Dev

How Do I Get First Collection Element Using Spacebars {{#if}} Tag?

From Dev

How do I insert an element at the correct position into a sorted array in Swift?

From Dev

How to get an input element that has an array name which I have assigned the value of the index?

From Dev

How could I get the first K element in scala Array?

From Dev

How do I split the getActionCommand() in order to get the first element in the string?

From Dev

How can I get the iteration of the first array index in a map?

From Dev

How do I get value of specific array element by secondary key?

From Dev

How do I find the index of an element in an array, vector or slice?

From Dev

How can I get the index of array element inside object?

From Dev

How do you get the index number of an array rather than the element?

From Dev

How do i get Object index in an array

From Dev

How do i get the value of first index in a returned List of array

From Dev

How do I get the index of an object in this array?

From Dev

PHP ARRAY get index of item for certain value

From Dev

How do I get the value in this neighboring element?

From Dev

PHP With 2 arrays how do I get first element that exists in both arrays based on the order of the first array?

From Dev

How do I add blank element to index[0] of an array

From Dev

How can I get an element in an array based on value rather than index with Json.NET in C#?

From Dev

How to find in sorted set an element which is just below a certain value

From Dev

how can i get first element in body index wise in jquery

From Dev

PHP - How do I unset an array index by value inside that index

From Dev

How to change element in array at certain index?

From Dev

How do I find the index of the first occurrence of a score in a sorted Redis set?

Related Related

  1. 1

    How do I loop through an array and stop at a certain index

  2. 2

    How do I check in JavaScript if a value exists at a certain array index?

  3. 3

    How do I get the first element while continue streaming?

  4. 4

    how do i insert element in knockout array at specific index

  5. 5

    How do I select the first sibling of a certain element type with jQuery?

  6. 6

    In Angular, how can I get the first element of a filtered array?

  7. 7

    How Do I Get First Collection Element Using Spacebars {{#if}} Tag?

  8. 8

    How do I insert an element at the correct position into a sorted array in Swift?

  9. 9

    How to get an input element that has an array name which I have assigned the value of the index?

  10. 10

    How could I get the first K element in scala Array?

  11. 11

    How do I split the getActionCommand() in order to get the first element in the string?

  12. 12

    How can I get the iteration of the first array index in a map?

  13. 13

    How do I get value of specific array element by secondary key?

  14. 14

    How do I find the index of an element in an array, vector or slice?

  15. 15

    How can I get the index of array element inside object?

  16. 16

    How do you get the index number of an array rather than the element?

  17. 17

    How do i get Object index in an array

  18. 18

    How do i get the value of first index in a returned List of array

  19. 19

    How do I get the index of an object in this array?

  20. 20

    PHP ARRAY get index of item for certain value

  21. 21

    How do I get the value in this neighboring element?

  22. 22

    PHP With 2 arrays how do I get first element that exists in both arrays based on the order of the first array?

  23. 23

    How do I add blank element to index[0] of an array

  24. 24

    How can I get an element in an array based on value rather than index with Json.NET in C#?

  25. 25

    How to find in sorted set an element which is just below a certain value

  26. 26

    how can i get first element in body index wise in jquery

  27. 27

    PHP - How do I unset an array index by value inside that index

  28. 28

    How to change element in array at certain index?

  29. 29

    How do I find the index of the first occurrence of a score in a sorted Redis set?

HotTag

Archive