Get previous true value from a given array index in Julia | Array findprev() Method
The findprev()
is an inbuilt function in julia which is used to return the previous index before or including i of a true element of the specified array A, or returns zero if true value is not found. Here values of index or key start from 1 i.e, for index of 1st element is 1, index of 2nd element is 2 and so on.
Syntax:
findprev(A, i)
or
findprev(predicate::Function, A, i)
Parameters:
- A: Specified array
- i: Specified value
- Predicate Function: Determines whether something is true or false based on the specified arguments
Returns: It returns the previous index before or including i of a true element of the specified array A, or returns zero if true value is not found.
Example 1:
A = [false, true, true, false]
println(findprev(A, 2 ))
B = [true false; true false]
println(findprev(B, CartesianIndex( 1 , 1 )))
C = cat([true false; true false],
[false true; true false],
[true false; true true], dims = 3 )
println(findprev(C, CartesianIndex( 1 , 2 , 1 )))
|
Output:
Example 2:
A = [ 1 , 2 , 5 , 6 ]
println(findprev(iseven, A, 3 ))
B = [ 3 5 ; 6 7 ]
println(findprev(iseven, B,
CartesianIndex( 2 , 2 )))
C = cat([ 1 2 ; 3 4 ], [ 5 6 ; 7 8 ],
[ 9 10 ; 11 12 ], dims = 3 )
println(findprev(iseven, C,
CartesianIndex( 2 , 2 , 1 )))
|
Output:
Last Updated :
23 Mar, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...