# Converting zero-based cartesian indices to one-based linear indices

**URL:** <https://discourse.julialang.org/t/converting-zero-based-cartesian-indices-to-one-based-linear-indices/18205>\
**Category:** General Usage\
**Tags:** indexing\
**Created:** [December 1, 2018, 3:40pm UTC](https://discourse.julialang.org/t/converting-zero-based-cartesian-indices-to-one-based-linear-indices/18205 "2018-12-01T15:40:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![damiendr](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@damiendr](https://discourse.julialang.org/u/damiendr)\
**Post date:** [December 1, 2018, 3:40pm UTC](https://discourse.julialang.org/t/converting-zero-based-cartesian-indices-to-one-based-linear-indices/18205/1 "2018-12-01T15:40:59Z")

</div>

I find myself needing to convert zero-based (or arbitrary) cartesian indices to one-based linear indices, such that, for instance, `indices[first(CartesianIndices((0:4,1:5)))] == 1`. I could have used the sub2ind function for that, but that now seems to be private. Instead I ended up writing a new type that looks suspiciously similar to LinearIndices:

```julia
struct CartesianSpace{A,N} <: AbstractArray{Int,N}
    axes::NTuple{N,A}
end
Base.axes(s::CartesianSpace) = s.axes
Base.IndexStyle(::Type{<:CartesianSpace}) = IndexLinear()
Base.size(s::CartesianSpace) = map(length, s.axes)
Base.getindex(s::CartesianSpace, i::Integer) = Int(i)

```

In fact the one significant difference is that axes(::LinearIndices) discards the original offset and always returns 1-based ranges. What’s the rationale for that? I’m confused that while Julia will let you build a LinearIndices((0:4,1:5)), this actually behaves as a LinearIndices((1:5,1:5)).
