findnext

findnext(A, i)

Find the next index >= i of a non-zero element of A, or 0 if not found.

Examples

  1. Find the next occurrence of a value in an array:

    julia> arr = [1, 2, 3, 4, 5, 2, 6, 7];
    julia> findnext(arr, 2, 3)
    6

    This example finds the next index greater than or equal to 3 where the value 2 is present in the array arr. It returns 6, which is the index of the next occurrence of 2 after index 3.

  2. Search for the next matching element in a sorted array:

    julia> sorted_nums = [1, 3, 5, 7, 9, 11, 13];
    julia> findnext(sorted_nums, 5, 2)
    3

    In this example, findnext is used to search for the next occurrence of 5 in the sorted array sorted_nums, starting from index 2. It returns 3, which is the index of the next occurrence of 5 after index 2.

  3. Find the next index of a character in a string:
    julia> text = "Hello, Julia!";
    julia> findnext(text, 'l', 5)
    7

    Here, findnext is applied to find the next index greater than or equal to 5 where the character 'l' is present in the string text. It returns 7, which is the index of the next occurrence of 'l' after index 5.

Common mistake example:

julia> arr = [1, 2, 3, 4, 5];
julia> findnext(arr, 6, 1)
0

In this example, the value 6 is not found in the array arr. As a result, findnext returns 0, indicating that the value was not found. Double-check that the value exists in the collection before using findnext to avoid such errors.

See Also

find, findfirst, findin, findlast, findmin, findn, findnext, findnz, findprev, rsearch, rsearchindex, searchsorted, searchsortedfirst, searchsortedlast, sort, sort!, sortcols, sortperm, sortperm!,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: