# Extracting indexes of vectors in Julia

**URL:** https://discourse.julialang.org/t/extracting-indexes-of-vectors-in-julia/78547
**Category:** General Usage
**Tags:** indexing, arrays
**Created:** [March 27, 2022, 1:12pm UTC](https://discourse.julialang.org/t/extracting-indexes-of-vectors-in-julia/78547 "2022-03-27T13:12:10Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Leticia-maria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leticia-maria/32/30981_2.png) [@Leticia-maria](https://discourse.julialang.org/u/Leticia-maria)
#### Post date: [March 27, 2022, 1:12pm UTC](https://discourse.julialang.org/t/extracting-indexes-of-vectors-in-julia/78547/1 "2022-03-27T13:12:11Z")

</div>

How to obtain the indexes of the nonzero terms of a vector in Julia?

```julia
x = [[3, 0, 0], [0, 4, 0], [5, 6, 0]]
output: ([0, 1, 2, 2]), [0, 1, 0, 1])

```

---

<div class="post-metadata">

### Author: ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)
#### Post date: [March 27, 2022, 1:37pm UTC](https://discourse.julialang.org/t/extracting-indexes-of-vectors-in-julia/78547/2 "2022-03-27T13:37:00Z")

</div>

Standard Julia Vectors/Arrays use indexing that starts at 1.

If all of your subvectors have the same length, you may prefer to work with a matrix:

```julia
M = vcat(x'...)
idx = M |> x->findall(!=(0),x)

```

Otherwise, you can write a loop:

```julia
function findNonzero(x)
       I,J = Int[],Int[]
       for (i,v) in enumerate(x)
           for (j,w) in enumerate(v)
               if w!=0
                   push!(I,i); push!(J,j)
               end
           end
       end
       return I,J
end

```

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [March 27, 2022, 1:58pm UTC](https://discourse.julialang.org/t/extracting-indexes-of-vectors-in-julia/78547/3 "2022-03-27T13:58:02Z")

</div>

You can get that exact output from

```julia
function f(x)
    a = [(i - 1, j - 1) for (i, r) in enumerate(x) for j in findall(!=(0), r)]
    return first.(a), last.(a)
end

```

but there’s a lot that’s unidiomatic about wanting that output so you should probably take a step back and look at a bigger picture of what you’re trying to accomplish.

> [@AndiMD](#):
>
> `idx = M |> x->findall(!=(0),x)`

I would just write that as `idx = findall(!=(0), M)`.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [March 27, 2022, 4:44pm UTC](https://discourse.julialang.org/t/extracting-indexes-of-vectors-in-julia/78547/4 "2022-03-27T16:44:31Z")

</div>

Another option that works for subvectors of arbitrary lengths (_kept Julia convention of indices starting from_ `1`):

```julia
function g(x)
    a = Int64[]; b = similar(a)
    for (i,xi) in pairs(x)
        idx = findall(!=(0), xi)
        !isempty(idx) && (for j in idx; push!(a,i); push!(b,j) end)
    end
    return a, b
end

```

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [March 27, 2022, 7:59pm UTC](https://discourse.julialang.org/t/extracting-indexes-of-vectors-in-julia/78547/5 "2022-03-27T19:59:46Z")

</div>

You can use `reduce(vcat,...)` for all tuples of nonzeros and then get `first` and `last` of them.

```julia
A = reduce(vcat, tuple.(i,findall(!=(0),j)) for (i,j) in pairs(x))
4-element Vector{Tuple{Int64, Int64}}:
 (1, 1)
 (2, 2)
 (3, 1)
 (3, 2)

first.(A)
4-element Vector{Int64}:
 1
 2
 3
 3

last.(A)
4-element Vector{Int64}:
 1
 2
 1
 2

```
