# CartesianIndex() - how to? - general questions

**URL:** <https://discourse.julialang.org/t/cartesianindex-how-to-general-questions/45630>\
**Category:** New to Julia\
**Tags:** question, indexing, cartesianindices\
**Created:** [August 27, 2020, 4:07pm UTC](https://discourse.julialang.org/t/cartesianindex-how-to-general-questions/45630 "2020-08-27T16:07:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![neo\_abs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neo_abs/32/16672_2.png) [@neo\_abs](https://discourse.julialang.org/u/neo_abs)\
**Post date:** [August 27, 2020, 4:07pm UTC](https://discourse.julialang.org/t/cartesianindex-how-to-general-questions/45630/1 "2020-08-27T16:07:35Z")

</div>

Is there a convenient way to:

1. Convert CaresianIndex into Vector{\<:Integer}
2. Convert CartesianIndex{4} into CartesianIndex{3} by removing last coordinate?
3. get k first values of Cartesian Index ( CI[1:4] → returns CartesianIndex{4} )

I know I can transform CI into tuple and work from there, but I wonder if there is some simpler way?

---

<div class="post-metadata">

**Author:** ![neo\_abs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neo_abs/32/16672_2.png) [@neo\_abs](https://discourse.julialang.org/u/neo_abs)\
**Post date:** [August 27, 2020, 4:25pm UTC](https://discourse.julialang.org/t/cartesianindex-how-to-general-questions/45630/2 "2020-08-27T16:25:07Z")

</div>

```julia
ind = CartesianIndices(zeros(2,2,2,2))
2×2×2×2 CartesianIndices{4,NTuple{4,Base.OneTo{Int64}}}:
[:, :, 1, 1] =
 CartesianIndex(1, 1, 1, 1) CartesianIndex(1, 2, 1, 1)
 CartesianIndex(2, 1, 1, 1) CartesianIndex(2, 2, 1, 1)
[:, :, 2, 1] =
 CartesianIndex(1, 1, 2, 1) CartesianIndex(1, 2, 2, 1)
 CartesianIndex(2, 1, 2, 1) CartesianIndex(2, 2, 2, 1)
[:, :, 1, 2] =
 CartesianIndex(1, 1, 1, 2) CartesianIndex(1, 2, 1, 2)
 CartesianIndex(2, 1, 1, 2) CartesianIndex(2, 2, 1, 2)
[:, :, 2, 2] =
 CartesianIndex(1, 1, 2, 2) CartesianIndex(1, 2, 2, 2)
 CartesianIndex(2, 1, 2, 2) CartesianIndex(2, 2, 2, 2)
julia> Tuple.(ind)
2×2×2×2 Array{NTuple{4,Int64},4}:
[:, :, 1, 1] =
 (1, 1, 1, 1) (1, 2, 1, 1)
 (2, 1, 1, 1) (2, 2, 1, 1)
[:, :, 2, 1] =
 (1, 1, 2, 1) (1, 2, 2, 1)
 (2, 1, 2, 1) (2, 2, 2, 1)
[:, :, 1, 2] =
 (1, 1, 1, 2) (1, 2, 1, 2)
 (2, 1, 1, 2) (2, 2, 1, 2)
[:, :, 2, 2] =
 (1, 1, 2, 2) (1, 2, 2, 2)
 (2, 1, 2, 2) (2, 2, 2, 2)

```

---

<div class="post-metadata">

**Author:** ![neo\_abs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neo_abs/32/16672_2.png) [@neo\_abs](https://discourse.julialang.org/u/neo_abs)\
**Post date:** [August 27, 2020, 4:26pm UTC](https://discourse.julialang.org/t/cartesianindex-how-to-general-questions/45630/3 "2020-08-27T16:26:17Z")

</div>

```julia
collect.(Tuple.(ind))
2×2×2×2 Array{Array{Int64,1},4}:
[:, :, 1, 1] =
 [1, 1, 1, 1] [1, 2, 1, 1]
 [2, 1, 1, 1] [2, 2, 1, 1]
[:, :, 2, 1] =
 [1, 1, 2, 1] [1, 2, 2, 1]
 [2, 1, 2, 1] [2, 2, 2, 1]
[:, :, 1, 2] =
 [1, 1, 1, 2] [1, 2, 1, 2]
 [2, 1, 1, 2] [2, 2, 1, 2]
[:, :, 2, 2] =
 [1, 1, 2, 2] [1, 2, 2, 2]
 [2, 1, 2, 2] [2, 2, 2, 2]

```

```julia
Tuple.(ind)[:]
16-element Array{NTuple{4,Int64},1}:
 (1, 1, 1, 1)
 (2, 1, 1, 1)
 (1, 2, 1, 1)
 (2, 2, 1, 1)
 (1, 1, 2, 1)
 (2, 1, 2, 1)
 (1, 2, 2, 1)
 (2, 2, 2, 1)
 (1, 1, 1, 2)
 (2, 1, 1, 2)
 (1, 2, 1, 2)
 (2, 2, 1, 2)
 (1, 1, 2, 2)
 (2, 1, 2, 2)
 (1, 2, 2, 2)
 (2, 2, 2, 2)

```

```julia
collect.(Tuple.(ind))[:]
16-element Array{Array{Int64,1},1}:
 [1, 1, 1, 1]
 [2, 1, 1, 1]
 [1, 2, 1, 1]
 [2, 2, 1, 1]
 [1, 1, 2, 1]
 [2, 1, 2, 1]
 [1, 2, 2, 1]
 [2, 2, 2, 1]
 [1, 1, 1, 2]
 [2, 1, 1, 2]
 [1, 2, 1, 2]
 [2, 2, 1, 2]
 [1, 1, 2, 2]
 [2, 1, 2, 2]
 [1, 2, 2, 2]
 [2, 2, 2, 2]

```

```julia
cat(collect.(Tuple.(ind))...,dims=2)
4×16 Array{Int64,2}:
 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2
 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2
 1 1 1 1 2 2 2 2 1 1 1 1 2 2 2 2
 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2

```

---

<div class="post-metadata">

**Author:** ![neo\_abs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neo_abs/32/16672_2.png) [@neo\_abs](https://discourse.julialang.org/u/neo_abs)\
**Post date:** [August 27, 2020, 4:54pm UTC](https://discourse.julialang.org/t/cartesianindex-how-to-general-questions/45630/4 "2020-08-27T16:54:40Z")

</div>

```julia
(ci->ntuple(i->ci[i],3)).(ind)
2×2×2×2 Array{Tuple{Int64,Int64,Int64},4}:
[:, :, 1, 1] =
 (1, 1, 1) (1, 2, 1)
 (2, 1, 1) (2, 2, 1)
[:, :, 2, 1] =
 (1, 1, 2) (1, 2, 2)
 (2, 1, 2) (2, 2, 2)
[:, :, 1, 2] =
 (1, 1, 1) (1, 2, 1)
 (2, 1, 1) (2, 2, 1)
[:, :, 2, 2] =
 (1, 1, 2) (1, 2, 2)
 (2, 1, 2) (2, 2, 2)

```

---

<div class="post-metadata">

**Author:** ![neo\_abs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neo_abs/32/16672_2.png) [@neo\_abs](https://discourse.julialang.org/u/neo_abs)\
**Post date:** [August 27, 2020, 5:28pm UTC](https://discourse.julialang.org/t/cartesianindex-how-to-general-questions/45630/5 "2020-08-27T17:28:47Z")

</div>

```julia
 (ci->CartesianIndex(ntuple(i->ci[i],3))).(ind)
2×2×2×2 Array{CartesianIndex{3},4}:
[:, :, 1, 1] =
 CartesianIndex(1, 1, 1) CartesianIndex(1, 2, 1)
 CartesianIndex(2, 1, 1) CartesianIndex(2, 2, 1)
[:, :, 2, 1] =
 CartesianIndex(1, 1, 2) CartesianIndex(1, 2, 2)
 CartesianIndex(2, 1, 2) CartesianIndex(2, 2, 2)
[:, :, 1, 2] =
 CartesianIndex(1, 1, 1) CartesianIndex(1, 2, 1)
 CartesianIndex(2, 1, 1) CartesianIndex(2, 2, 1)
[:, :, 2, 2] =
 CartesianIndex(1, 1, 2) CartesianIndex(1, 2, 2)
 CartesianIndex(2, 1, 2) CartesianIndex(2, 2, 2)

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [August 27, 2020, 5:37pm UTC](https://discourse.julialang.org/t/cartesianindex-how-to-general-questions/45630/6 "2020-08-27T17:37:20Z")

</div>

You could also use `Base.front` instead of `ntuple(i->ci[i], 3)` here.
