findnext
findnext(A, i)
Find the next index >= i of a non-zero element of A, or 0 if not found.
Examples
-
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) 6This example finds the next index greater than or equal to
3where the value2is present in the arrayarr. It returns6, which is the index of the next occurrence of2after index3. -
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) 3In this example,
findnextis used to search for the next occurrence of5in the sorted arraysorted_nums, starting from index2. It returns3, which is the index of the next occurrence of5after index2. - Find the next index of a character in a string:
julia> text = "Hello, Julia!"; julia> findnext(text, 'l', 5) 7Here,
findnextis applied to find the next index greater than or equal to5where the character'l'is present in the stringtext. It returns7, which is the index of the next occurrence of'l'after index5.
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.