Get index of first true value of array in Julia | Array findfirst() Method
Last Updated :
23 Mar, 2020
The findfirst()
is an inbuilt function in julia which is used to return the index or key of the first true value in the specified array. 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:
findfirst(A)
or
findfirst(predicate::Function, A)
Parameters:
- A: Specified array
- Predicate Function: Determines whether something is true or false based on the specified arguments
Returns: It returns the index or key of the first true value in the specified array.
Example 1:
A = [false, true, true, false]
println(findfirst(A))
B = [false false; true false]
println(findfirst(B))
C = cat([false false; true false],
[false true; true false],
[true false; true true], dims = 3 )
println(findfirst(C))
|
Output:
Example 2:
A = [ 1 , 2 , 5 , 6 ]
println(findfirst(iseven, A))
B = [ 3 5 ; 6 7 ]
println(findfirst(iseven, B))
C = cat([ 1 2 ; 3 4 ], [ 5 6 ; 7 8 ],
[ 9 10 ; 11 12 ], dims = 3 )
println(findfirst(iseven, C))
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...