# Obtain cartesian indices from dimensions only

**URL:** <https://discourse.julialang.org/t/obtain-cartesian-indices-from-dimensions-only/62366>\
**Category:** General Usage\
**Created:** [June 3, 2021, 11:31pm UTC](https://discourse.julialang.org/t/obtain-cartesian-indices-from-dimensions-only/62366 "2021-06-03T23:31:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 3, 2021, 11:31pm UTC](https://discourse.julialang.org/t/obtain-cartesian-indices-from-dimensions-only/62366/1 "2021-06-03T23:31:35Z")

</div>

I can obtain the cartesian indices of a matrix from a linear index with:

```julia
julia> A = zeros(2,4,3);

julia> CartesianIndices(A)[5]
CartesianIndex(1, 3, 1)

```

I would want to obtain that without having to define `A`, that is, only from its dimensions. Something like:

```julia
CartesianIndices(2,4,3)[5]

```

Is that already explicitly implemented? Or can I define a “fake” matrix only to input it there?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 3, 2021, 11:46pm UTC](https://discourse.julialang.org/t/obtain-cartesian-indices-from-dimensions-only/62366/2 "2021-06-03T23:46:14Z")

</div>

you can always define a fake array type that has a 0 cost constructor.

```julia
struct OnlyZeros{T,N} <: AbstractArray{T,N}
    size::NTuple{N,Int}
end
OnlyZeros{T}(size::NTuple{N,Int}) where {N,T} = OnlyZeros{T,N}(size)
OnlyZeros(size::NTuple{N,Int}) where N = OnlyZeros{Int,N}(size) 
Base.size(A::OnlyZeros) = A.size
Base.getindex(A::OnlyZeros{T,N}, inds::Vararg{Int,N}) where {T,N} = zero(T)

```

And then you can write  
`CartesianIndices(OnlyZeros(2,4,3))[5]`

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 3, 2021, 11:50pm UTC](https://discourse.julialang.org/t/obtain-cartesian-indices-from-dimensions-only/62366/3 "2021-06-03T23:50:53Z")

</div>

Thank you! Indeed, I was thinking of something like that.

Yet I am thinking why that is not already defined (an `EmptyArray` type). Probably if nobody never needed that I might have to rethink the way I have thought my problem. 😬

---

<div class="post-metadata">

**Author:** ![RJDennis](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@RJDennis](https://discourse.julialang.org/u/RJDennis)\
**Post date:** [June 3, 2021, 11:59pm UTC](https://discourse.julialang.org/t/obtain-cartesian-indices-from-dimensions-only/62366/4 "2021-06-03T23:59:57Z")

</div>

What about:

```julia
CartesianIndices((2,4,3))[5]

```

?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 4, 2021, 12:03am UTC](https://discourse.julialang.org/t/obtain-cartesian-indices-from-dimensions-only/62366/5 "2021-06-04T00:03:36Z")

</div>

Ah, if that works that might explain why there is no such thing as the EmptyArray type.
