# CartesianIndex to Arrays of index

**URL:** <https://discourse.julialang.org/t/cartesianindex-to-arrays-of-index/23496>\
**Category:** New to Julia\
**Tags:** question, cartesianindices\
**Created:** [April 25, 2019, 7:40am UTC](https://discourse.julialang.org/t/cartesianindex-to-arrays-of-index/23496 "2019-04-25T07:40:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yusri-dh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yusri-dh/32/7404_2.png) [@yusri-dh](https://discourse.julialang.org/u/yusri-dh)\
**Post date:** [April 25, 2019, 7:40am UTC](https://discourse.julialang.org/t/cartesianindex-to-arrays-of-index/23496/1 "2019-04-25T07:40:45Z")

</div>

Dear all, I have some problems with CartesianIndex. In numpy when I want to index of nonzero elements I can do like this:

```julia
>>> x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
>>> x
array([[3, 0, 0],
       [0, 4, 0],
       [5, 6, 0]])
>>> a,b=np.nonzero(x)
>>> a
array([0, 1, 2, 2])
>>> b
array([0, 1, 0, 1]))

```

In Julia

```julia
>>> x = [3 0 0; 0 4 0; 5 6 0]
3×3 Array{Int64,2}:
 3 0 0
 0 4 0
 5 6 0
>>> findall(x.>0)
4-element Array{CartesianIndex{2},1}:
 CartesianIndex(1, 1)
 CartesianIndex(3, 1)
 CartesianIndex(2, 2)
 CartesianIndex(3, 2)

```

How can I get an variable a and b like in Python where a = [1,3,2,3] and b = [1,1,2,2]?

---

<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:** [April 25, 2019, 10:54am UTC](https://discourse.julialang.org/t/cartesianindex-to-arrays-of-index/23496/3 "2019-04-25T10:54:54Z")

</div>

Firstly, if you want all non-zero, you should write `findall(!iszero, x)`, since `x.>0` will give you just positive-valued elements.

Secondly, you can convert and extract `a` and `b` like this:

```julia
julia> inds = Tuple.(findall(!iszero, x))
4-element Array{Tuple{Int64,Int64},1}:
 (1, 1)
 (3, 1)
 (2, 2)
 (3, 2)

julia> a = first.(inds)
4-element Array{Int64,1}:
 1
 3
 2
 3

julia> b = last.(inds)
4-element Array{Int64,1}:
 1
 1
 2
 2

```

I think it is a bit risky to rely on access to internal fields, like `.I`, it’s better to convert to `Tuple`.

Thirdly, you should ask yourself if this is really necessary. Why do you need `a` and `b` separately? Cartesian indices are constructed in such a way that they should be easy and convenient to use without destructuring them like this. Perhaps you have a good reason, but sometimes Matlab and Python users ask for this because they are not used to the way `CartesianIndex` works.

---

<div class="post-metadata">

**Author:** ![yusri-dh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yusri-dh/32/7404_2.png) [@yusri-dh](https://discourse.julialang.org/u/yusri-dh)\
**Post date:** [April 25, 2019, 2:33pm UTC](https://discourse.julialang.org/t/cartesianindex-to-arrays-of-index/23496/4 "2019-04-25T14:33:29Z")

</div>

Thank you. It is working well. You are right, I am Python users so I am not sure how to do it in Julia way. I need a and b separately because I want to make plot from it e.g.

```julia
Using Plots

plot(a,b)

```

As far as I know, CartesianIndex can not be used as input in plot function so thats why I need a and b separately.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 25, 2019, 3:56pm UTC](https://discourse.julialang.org/t/cartesianindex-to-arrays-of-index/23496/5 "2019-04-25T15:56:26Z")

</div>

> [@DNF](#):
>
> I think it is a bit risky to rely on access to internal fields, like `.I` , it’s better to convert to `Tuple` .

`CartesianIndex` supports `getindex` (i.e. `i[j]`), so you can do:

```julia
inds = findall(!iszero, x)
a = getindex.(inds, 1)
b = getindex.(inds, 2)

```

---

<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:** [April 25, 2019, 6:04pm UTC](https://discourse.julialang.org/t/cartesianindex-to-arrays-of-index/23496/6 "2019-04-25T18:04:49Z")

</div>

That would work _really_ well with a hypothetical `getindex` dot syntax:

```julia
a = inds.[1] 
b = inds.[2] 

```

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [April 26, 2019, 7:08am UTC](https://discourse.julialang.org/t/cartesianindex-to-arrays-of-index/23496/7 "2019-04-26T07:08:31Z")

</div>

Although `plot` currently doesn’t support `CartesianIndex`s, it does support `Tuple`s, so after

```julia
inds = Tuple.(findall(!iszero, x))

```

there’s no need to “unzip” the vector-of-tuples `inds` into `a` and `b`. It is possible to

```julia
plot(inds)

```

directly.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [April 26, 2019, 7:43am UTC](https://discourse.julialang.org/t/cartesianindex-to-arrays-of-index/23496/8 "2019-04-26T07:43:21Z")

</div>

If one wants to stick to the convention of putting the first index on the y-axis, starting at the top, then one could write:

```julia
plot(reverse.(inds), yaxis=:flip)

```
