# Obtain cartesian indices from dimensions only

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

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

julia> CartesianIndices(A)
CartesianIndex(1, 3, 1)

``````

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

``````CartesianIndices(2,4,3)
``````

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

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

``````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))`

2 Likes

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. 1 Like

``````CartesianIndices((2,4,3))