Finding Position of Element in an Array

`findall` is the name in version 0.7. In version 0.6 you just use `find` instead.

2 Likes

I used find and i get the following:

find(x->x==2, A)
10-element Array{Int64,1}:
1
5
10
11
12
14
17
19
26
27

What i need is the position (CartesianIndex). What am I doing wrong?

…i want to obtain the position (i,j,k)of a particular number (element) in that array.

`ind2sub(A, find(A .== 2))`

EDIT (for v0.6)

``````i, j, k = ind2sub(A, find(x->x == 2,A))
idx = [i  j  k]
``````
2 Likes

Is there a mode for `findall()` in 0.7 which works with linear indices?

It is normally preferable to use `find(x->x==2, A)` over `find(A.==2)`.

1 Like

You can convert, something like

``````julia> v = rand(3,3);

julia> C = findall(x->x>0.5, v)
5-element Array{CartesianIndex{2},1}:
CartesianIndex(2, 1)
CartesianIndex(2, 2)
CartesianIndex(3, 2)
CartesianIndex(2, 3)
CartesianIndex(3, 3)

julia> L = LinearIndices(v)
3×3 LinearIndices{2,Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}}:
1  4  7
2  5  8
3  6  9

julia> L[C]
5-element Array{Int64,1}:
2
5
6
8
9
``````
1 Like

As wrote Kristoffer:

`(LinearIndices(A))[findall(x->x == 2, A)]`

You can also do

``````julia> C = findall(x->x>0.5, vec(v))
3-element Array{Int64,1}:
1
2
4
``````
4 Likes

Is it efficient?
Could we not add mode for `findall()` to iterate to through all elements in one loop?

Does Julia have more efficient way to work on the array (Like looping on its length)?

I think Kristoffer solved this problem:

Thanks to you all. Since I don,'t have findall, I tried `ind2sub(A, find(A == 2))` and it worked!!

How does it compare to something like:

`y = [i for i ∈ 1:length(x) if x[i] > 0.5];`

Try it and see?
[Put both in functions and use `BenchmarkTools.jl`.]

1 Like

It seems that “find” is not available anymore in Julia 1.0, correct?

If so, how do I find the index of an element in a vector?

Such as: a = [ “a”, “b”, “c”, “d” ]

find “3” for the index of element “c”.

Thank you. (if there is any particularly efficient intrinsic function to do so, of course).

`findfirst` or `findall`, both of which are well-documented with examples

1 Like

I tried to use findfirst, but it seems that it only works for string inputs, not vector inputs.

``````julia> a = [ "a", "b", "c", "d" ]
4-element Array{String,1}:
"a"
"b"
"c"
"d"

julia> findfirst("c",a)
ERROR: MethodError: no method matching findfirst(::String, ::Array{String,1})

``````

Maybe `findfirst((x -> x=="c"), a)`? (Slightly contrived, there might be a better way?)

1 Like

But you still didn’t read the documentation… this would work:

``````a = [ "a", "b", "c", "d" ]
findfirst(isequal("c"), a)
``````
6 Likes

Slightly neater and more legible than my reinvention of the `isequal` function …

Thank you.

But there is a difference between not reading the documentation, which is in the link below and does not mention “isequal” anywhere, and not knowing how to combine different functions to obtain the desired result.

https://docs.julialang.org/en/v1/base/strings/#Base.findfirst-Tuple{AbstractString,AbstractString}