# Slicing and merging of CartesianIndices

**URL:** https://discourse.julialang.org/t/slicing-and-merging-of-cartesianindices/61519
**Category:** General Usage
**Tags:** indexing
**Created:** [May 20, 2021, 1:55pm UTC](https://discourse.julialang.org/t/slicing-and-merging-of-cartesianindices/61519 "2021-05-20T13:55:11Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![fedoroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedoroff/32/53209_2.png) [@fedoroff](https://discourse.julialang.org/u/fedoroff)
#### Post date: [May 20, 2021, 1:55pm UTC](https://discourse.julialang.org/t/slicing-and-merging-of-cartesianindices/61519/1 "2021-05-20T13:55:11Z")

</div>

Hello,

Let’s assume I have a two-dimensional array and the list of Cartesian indices which allow me to extract some subset of the array elements:

```julia
A = zeros((6, 9))

ci = CartesianIndices((2:2:6, 3:3:9))

A[ci]

```

Now, having `ci`, how I can extract the Cartesian indices which correspond separately to the first and second dimensions of the array? If I will define them manually, they will be

```julia
ci1 = CartesianIndices((2:2:6,))
ci2 = CartesianIndices((3:3:9,))

A[ci1, :]
A[:, ci2]

```

And the other way around, having `ci1` and `ci2`, how I can merge them into the original `ci`?  
The last question is related to my earlier post [https://discourse.julialang.org/t/combine-cartesianindices-for-effective-cuda-kernels/](https://discourse.julialang.org/t/combine-cartesianindices-for-effective-cuda-kernels/)

Thank you.

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [May 20, 2021, 4:15pm UTC](https://discourse.julialang.org/t/slicing-and-merging-of-cartesianindices/61519/2 "2021-05-20T16:15:14Z")

</div>

You can do use `ci.indices` though I think that’s an undocumented internal:

```julia
ci1 = CartesianIndices(ci.indices[1])
ci2 = CartesianIndices(ci.indices[2])

```

You can get the same information with `first(ci)`, `step(ci)`, `last(ci)` but that’s less convenient.

To recover the full list:

```julia
CartesianIndices((ci1.indices[1], ci2.indices[1]))

```

Another idea using arrays of `CartesianIndex` rather than `CartesianIndices`:

```julia
julia> i1 = getindex.(ci[:, 1], 1)
3-element Vector{Int64}:
 2
 4
 6

julia> i2 = getindex.(ci[1, :], 2)
3-element Vector{Int64}:
 3
 6
 9

julia> CartesianIndex.(i1, i2')
3×3 Matrix{CartesianIndex{2}}:
 CartesianIndex(2, 3) CartesianIndex(2, 6) CartesianIndex(2, 9)
 CartesianIndex(4, 3) CartesianIndex(4, 6) CartesianIndex(4, 9)
 CartesianIndex(6, 3) CartesianIndex(6, 6) CartesianIndex(6, 9)

```

---

<div class="post-metadata">

### Author: ![fedoroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedoroff/32/53209_2.png) [@fedoroff](https://discourse.julialang.org/u/fedoroff)
#### Post date: [May 20, 2021, 4:28pm UTC](https://discourse.julialang.org/t/slicing-and-merging-of-cartesianindices/61519/3 "2021-05-20T16:28:56Z")

</div>

Thank you.  
`indices` is exactly what I need.
