findprev(A,i)
findprev(A, i)
Find the previous index <= i of a non-zero element of A, or 0 if not found.
Examples
- 
Find the previous index of a matching element in an array:
julia> arr = [1, 2, 3, 4, 5, 6]; julia> findprev(x -> x > 3, arr, 5) 3In this example, the
findprevfunction is used to find the index of the previous element inarrthat is greater than3. - 
Find the previous index of a matching element in a vector of strings:
julia> words = ["apple", "banana", "orange", "grape"]; julia> findprev(x -> length(x) > 5, words, 4) 2This example demonstrates finding the index of the previous string in
wordsthat has a length greater than5. - Handle cases where no matching element is found:
julia> numbers = [1, 2, 3, 4]; julia> findprev(x -> x == 5, numbers, 3) 0In this case, the
findprevfunction returns0since there is no element innumbersthat satisfies the predicate. 
Common mistake example:
julia> arr = [10, 20, 30, 40];
julia> findprev(x -> x > 50, arr, 4)
ERROR: BoundsError: attempt to access 4-element Array{Int64,1} at index [5]
In this example, an IndexError occurs because the predicate never returns true, causing the function to search beyond the bounds of the array. It is essential to ensure that the predicate has a matching condition 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.