# CartesianIndex to an integer vector

**URL:** <https://discourse.julialang.org/t/cartesianindex-to-an-integer-vector/127338>\
**Category:** New to Julia\
**Tags:** cartesianindices\
**Created:** [March 25, 2025, 10:40am UTC](https://discourse.julialang.org/t/cartesianindex-to-an-integer-vector/127338 "2025-03-25T10:40:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![seismobassoon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/seismobassoon/32/211478_2.png) [@seismobassoon](https://discourse.julialang.org/u/seismobassoon)\
**Post date:** [March 25, 2025, 10:40am UTC](https://discourse.julialang.org/t/cartesianindex-to-an-integer-vector/127338/1 "2025-03-25T10:40:07Z")

</div>

I have a simple question, rather esthetic than essential: I need to use CartesianIndex as an integer vector. The code below does work with “collect+Tuple” but collect, Vector alone did not. Do you have any idea?

julia\> a=rand(3,4,5) ; R = CartesianIndices(a);

julia\> offset = collect(Tuple(R[10]-R[2]))  
3-element Vector{Int64}:  
-1  
3  
0

---

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [March 25, 2025, 11:23am UTC](https://discourse.julialang.org/t/cartesianindex-to-an-integer-vector/127338/2 "2025-03-25T11:23:24Z")

</div>

If you try try to execute `collect(CartesianIndex(1,2,3))` the error will say that this is deliberately not supported. My guess would be that conceptually, a `CartesianIndex` is not really equivalent to a `Vector` and that supporting this operation would also make certain bugs too easy to accidentally occur.

If this is a big pain point for you, then you could write your own function to do this, e.g.:

```julia
car2vec(x::CartesianIndex) = collect(Tuple(x))

```

Writing your own `collect` method to do this would be type piracy and therefore not recommended.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [March 25, 2025, 11:55am UTC](https://discourse.julialang.org/t/cartesianindex-to-an-integer-vector/127338/3 "2025-03-25T11:55:42Z")

</div>

> [@GHTaarn](#):
>
> conceptually, a `CartesianIndex` is not really equivalent to a `Vector`

To generalize, a composite data structure isn’t necessarily multiple elements semantically, let alone iterable. `Complex{T}` is a composite of 2 `T` fields, but we wouldn’t expect to `collect` it into `Vector{T}` with 2 elements; `Complex` is iterable so `collect` works, but it is 1 element like a typical `Number`. Of course, anything implemented with multiple elements can be processed to something that is semantically a collection; `getfield.(::C, 1:fieldcount(C))` would collect the fields of a struct `C`, so it works on `Complex`.

Back to the topic at hand, there’s no cost to `Tuple(::CartesianIndex)` because it’s just a simple getter for the `Tuple` that `CartesianIndex` is built on: `Tuple(index::CartesianIndex) = index.I`.

---

<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:** [March 25, 2025, 2:41pm UTC](https://discourse.julialang.org/t/cartesianindex-to-an-integer-vector/127338/4 "2025-03-25T14:41:31Z")

</div>

Often, the `collect` in `collect(Tuple(...))` is not needed for the subsequent processing:

```julia
julia> Tuple(R[10]-R[2])
(-1, 3, 0)

```

should be enough.

Note that a Vector can have a run-time only known size, while a Tuple has compile-time known size. In this case using a Tuple will preserve this compile-time knowledge and allow better optimization.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [March 25, 2025, 3:33pm UTC](https://discourse.julialang.org/t/cartesianindex-to-an-integer-vector/127338/5 "2025-03-25T15:33:59Z")

</div>

If methods only work for `Array`, then there’s no choice. Many methods work on `AbstractArray` though, and while `Tuple` won’t work, `SVector` from StaticArrays.jl can simply wrap it, keeping all the benefits of immutability and a small compile-time size.

---

<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:** [March 25, 2025, 3:34pm UTC](https://discourse.julialang.org/t/cartesianindex-to-an-integer-vector/127338/6 "2025-03-25T15:34:05Z")

</div>

I’d second the recommendation that you consider using the `Tuple` directly, rather than converting it to a `Vector` (which has a considerable performance penalty at this size).

_edit: the above poster narrowly beat me to the `SVector` recommendation_

If you need to treat the values as a mathematical vector (rather than simply an indexable collection), consider the StaticArrays.jl package’s `SVector`, which is specially designed for small fixed-size vectors like you’re considering here. These have virtually no performance difference from `Tuple` (since they basically are one) and support almost all the linear algebra that `Vector` does (except mutation, for which `MVector` exists but sometimes has somewhat less performance).

```julia-repl
julia> using StaticArrays

julia> SVector(Tuple(CartesianIndex(4,-1,2)))
3-element SVector{3, Int64} with indices SOneTo(3):
  4
 -1
  2

```

---

<div class="post-metadata">

**Author:** ![seismobassoon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/seismobassoon/32/211478_2.png) [@seismobassoon](https://discourse.julialang.org/u/seismobassoon)\
**Post date:** [March 26, 2025, 11:26am UTC](https://discourse.julialang.org/t/cartesianindex-to-an-integer-vector/127338/7 "2025-03-26T11:26:30Z")

</div>

Thanks, I used your car2vec 😉

---

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [March 26, 2025, 2:11pm UTC](https://discourse.julialang.org/t/cartesianindex-to-an-integer-vector/127338/8 "2025-03-26T14:11:06Z")

</div>

The following may be a reason why Julia does not have a standard conversion from `CartesianIndex` to `Vector`

```julia-repl
julia> m=reshape(1:16,4,4)
4×4 reshape(::UnitRange{Int64}, 4, 4) with eltype Int64:
 1 5 9 13
 2 6 10 14
 3 7 11 15
 4 8 12 16

julia> m[[1,2]]
2-element Vector{Int64}:
 1
 2

julia> m[CartesianIndex(1,2)]
5

```
