# Finding Position of Element in an Array

**URL:** https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502
**Category:** General Usage
**Tags:** question
**Created:** [July 19, 2018, 4:25pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502 "2018-07-19T16:25:22Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![jazzieman](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jazzieman](https://discourse.julialang.org/u/jazzieman)
#### Post date: [July 19, 2018, 4:25pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/1 "2018-07-19T16:25:22Z")

</div>

I’m looking for a function (is there is one) where i can get the position of an element in an array? I have a 3D array of numbers and i want to obtain the position (i,j,k)of a particular number (element) in that array. Is there a way to obtain it? Thanks in advance.

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [July 19, 2018, 4:26pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/2 "2018-07-19T16:26:32Z")

</div>

The following works on Julia 0.6:

```julia
julia> A = rand(1:3, 3, 3, 3)
3×3×3 Array{Int64,3}:
[:, :, 1] =
 2 3 2
 2 2 2
 2 1 3

[:, :, 2] =
 3 3 3
 2 1 3
 3 1 1

[:, :, 3] =
 3 2 1
 2 2 2
 3 1 1

julia> findall(x->x==2, A)
11-element Array{CartesianIndex{3},1}:
 CartesianIndex(1, 1, 1)
 CartesianIndex(2, 1, 1)
 CartesianIndex(3, 1, 1)
 CartesianIndex(2, 2, 1)
 CartesianIndex(1, 3, 1)
 CartesianIndex(2, 3, 1)
 CartesianIndex(2, 1, 2)
 CartesianIndex(2, 1, 3)
 CartesianIndex(1, 2, 3)
 CartesianIndex(2, 2, 3)
 CartesianIndex(2, 3, 3)

```

---

<div class="post-metadata">

### Author: ![jazzieman](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jazzieman](https://discourse.julialang.org/u/jazzieman)
#### Post date: [July 19, 2018, 5:22pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/3 "2018-07-19T17:22:18Z")

</div>

> [@dpsanders](#):
>
> findall(x-\>x==2, A)

Thank you for the prompt response. I tried it but i get the following error:

_findall(x-\>x==2, A)_  
_ERROR: UndefVarError: findall not defined_

Apratently i dont have that function. When i look the function it tells me:

Binding findall does not exis

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [July 19, 2018, 5:45pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/4 "2018-07-19T17:45:19Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jazzieman](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jazzieman](https://discourse.julialang.org/u/jazzieman)
#### Post date: [July 19, 2018, 6:01pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/5 "2018-07-19T18:01:45Z")

</div>

> [@dpsanders](#):
>
> findall(x-\>x==2, A)

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?

---

<div class="post-metadata">

### Author: ![abobroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abobroff/32/3752_2.png) [@abobroff](https://discourse.julialang.org/u/abobroff)
#### Post date: [July 20, 2018, 5:39am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/6 "2018-07-20T05:39:54Z")

</div>

> …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)

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

```

---

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [July 20, 2018, 6:31am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/7 "2018-07-20T06:31:41Z")

</div>

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

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [July 20, 2018, 7:38am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/8 "2018-07-20T07:38:47Z")

</div>

> [@abobroff](#):
>
> `ind2sub(A, find(A .== 2))`

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

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [July 20, 2018, 8:30am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/9 "2018-07-20T08:30:18Z")

</div>

You can convert, something like

```julia
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

```

---

<div class="post-metadata">

### Author: ![abobroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abobroff/32/3752_2.png) [@abobroff](https://discourse.julialang.org/u/abobroff)
#### Post date: [July 20, 2018, 10:02am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/10 "2018-07-20T10:02:43Z")

</div>

> [@RoyiAvital](#):
>
> Is there a mode for `findall()` in 0.7 which works with linear indices?

As wrote Kristoffer:

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

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [July 20, 2018, 10:34am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/11 "2018-07-20T10:34:12Z")

</div>

You can also do

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

```

---

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [July 20, 2018, 10:35am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/12 "2018-07-20T10:35:16Z")

</div>

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)?

---

<div class="post-metadata">

### Author: ![abobroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abobroff/32/3752_2.png) [@abobroff](https://discourse.julialang.org/u/abobroff)
#### Post date: [July 20, 2018, 11:03am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/13 "2018-07-20T11:03:36Z")

</div>

I think Kristoffer solved this problem:

> [@kristoffer.carlsson](#):
>
> C = findall(x-\>x\>0.5, vec(v))

---

<div class="post-metadata">

### Author: ![jazzieman](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jazzieman](https://discourse.julialang.org/u/jazzieman)
#### Post date: [July 20, 2018, 3:48pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/14 "2018-07-20T15:48:12Z")

</div>

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

---

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [July 20, 2018, 4:49pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/15 "2018-07-20T16:49:52Z")

</div>

> [@abobroff](#):
>
> I think Kristoffer solved this problem:
> 
> > [@kristoffer.carlsson](#):
> >
> > C = findall(x-\>x\>0.5, vec(v))

How does it compare to something like:

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

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [July 20, 2018, 5:32pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/16 "2018-07-20T17:32:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [February 4, 2019, 4:51pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/17 "2019-02-04T16:51:30Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [February 4, 2019, 5:01pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/18 "2019-02-04T17:01:05Z")

</div>

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

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [February 4, 2019, 5:13pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/19 "2019-02-04T17:13:08Z")

</div>

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

```julia
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})

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [February 4, 2019, 5:20pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/20 "2019-02-04T17:20:25Z")

</div>

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

[Next page](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502.md?page=2)
