# Best way to drop an entry of a CartesianIndex?

**URL:** https://discourse.julialang.org/t/best-way-to-drop-an-entry-of-a-cartesianindex/104127
**Category:** New to Julia
**Created:** [September 21, 2023, 11:27pm UTC](https://discourse.julialang.org/t/best-way-to-drop-an-entry-of-a-cartesianindex/104127 "2023-09-21T23:27:33Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jeffreyesun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreyesun/32/13379_2.png) [@jeffreyesun](https://discourse.julialang.org/u/jeffreyesun)
#### Post date: [September 21, 2023, 11:27pm UTC](https://discourse.julialang.org/t/best-way-to-drop-an-entry-of-a-cartesianindex/104127/1 "2023-09-21T23:27:33Z")

</div>

Out of curiosity, what is the best way to drop one entry of an `idx::CartesianIndex`? Or, generally, to access a slice of it. It’s forbidden to do `idx[2:5]`, so right now I’m doing:

`CartesianIndex(Tuple(idx)[2:5])`.

Just curious if this is the best way to do this.

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [September 21, 2023, 11:38pm UTC](https://discourse.julialang.org/t/best-way-to-drop-an-entry-of-a-cartesianindex/104127/2 "2023-09-21T23:38:14Z")

</div>

That works. You can also do the following which may be slightly shorter.

```julia
julia> idx = CartesianIndex((1,2,3,4,5))
CartesianIndex(1, 2, 3, 4, 5)

julia> CartesianIndex(idx.I[2:5])
CartesianIndex(2, 3, 4, 5)

```

Your version is arguably better though as it does not depend on the implementation details of `CartesianIndex`.

A better answer might come if you tell us what larger problem you are trying to solve.

---

<div class="post-metadata">

### Author: ![jeffreyesun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreyesun/32/13379_2.png) [@jeffreyesun](https://discourse.julialang.org/u/jeffreyesun)
#### Post date: [September 22, 2023, 12:26am UTC](https://discourse.julialang.org/t/best-way-to-drop-an-entry-of-a-cartesianindex/104127/3 "2023-09-22T00:26:07Z")

</div>

Thanks! This definitely works for me, as much as I’m tempted to overload `Base.getindex` and introduce all kinds of weird bugs into my project.

My particular problem is just that I have a `a::Array{Float64, 5}` and I want to get the whole “column” containing a particular index `idx::CartesianIndex{5}`.

So basically, right now I’m doing

```julia
function get_column_containing(A, idx)
  return A[:, CartesianIndex(Tuple(idx)[2:5])
end

```

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [September 22, 2023, 5:04am UTC](https://discourse.julialang.org/t/best-way-to-drop-an-entry-of-a-cartesianindex/104127/4 "2023-09-22T05:04:24Z")

</div>

There is also:

```julia
Base.tail(Tuple(idx))

```

which may be a little more self documenting.

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [September 22, 2023, 3:07pm UTC](https://discourse.julialang.org/t/best-way-to-drop-an-entry-of-a-cartesianindex/104127/5 "2023-09-22T15:07:09Z")

</div>

You may also find `Base.setindex` (not to be confused with the more popular `Base.setindex!`) to be useful, since stripping/replacing things in the middle (if you have that need) can be more tedious.

```julia
julia> idx = CartesianIndex(1,2,3,4,5);

julia> Base.setindex(Tuple(idx),:,2)
(1, Colon(), 3, 4, 5)

julia> A[Base.setindex(Tuple(idx),:,2)...]

```

Note that this call to `setindex` is technically type-unstable, but if the index is a compile time constant then it will usually resolve itself via constant propagation (compare `@code_warntype Base.setindex(Tuple(idx),:,2)` to `@code_warntype (i->Base.setindex(Tuple(i),:,2))(idx)`). The same thing appears to happen with `CartesianIndex(Tuple(idx)[2:5])`, where the `2:5` would normally cause a type instability (`length(2:5)` is not a part of the type of `2:5`, but `length` is important to `Tuple`) but is resolved via constant propagation.
