# What is the proper way to construct CartesianIndices from CartesianIndex in Julia 1.0?

**URL:** <https://discourse.julialang.org/t/what-is-the-proper-way-to-construct-cartesianindices-from-cartesianindex-in-julia-1-0/15579>\
**Category:** General Usage\
**Tags:** question, indexing\
**Created:** [September 27, 2018, 9:00pm UTC](https://discourse.julialang.org/t/what-is-the-proper-way-to-construct-cartesianindices-from-cartesianindex-in-julia-1-0/15579 "2018-09-27T21:00:47Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [September 30, 2018, 12:22pm UTC](https://discourse.julialang.org/t/what-is-the-proper-way-to-construct-cartesianindices-from-cartesianindex-in-julia-1-0/15579/2 "2018-09-30T12:22:51Z")

</div>

> isn’t it better to use CartesianIndex all the way down? Transforming to UnitRange make the code not quite clean.

Some things turn out to be easier with the range representation, and because [ranges are sometimes used to encode the array type for unconventional indexing](https://docs.julialang.org/en/v1/devdocs/offset-arrays/#Custom-AbstractUnitRange-types-1) the new representation is the only truly correct approach because it allows you to preserve the type of ranges.

However, the lack of a good default for construction from `CartesianIndex` is an oversight that should be corrected (hopefully in julia 1.1, see [Add colon constructor for CartesianIndices by timholy · Pull Request #29440 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/29440)). (See also [PSA: replacement of ind2sub/sub2ind in Julia 0.7+ - #6 by mauro3](https://discourse.julialang.org/t/psa-replacement-of-ind2sub-sub2ind-in-julia-0-7/14666/6)). In the meantime I use this:

```julia
_colon(I::CartesianIndex{N}, J::CartesianIndex{N}) where N =
    CartesianIndices(map((i,j) -> i:j, Tuple(I), Tuple(J)))

```

---

_[View the full topic](https://discourse.julialang.org/t/what-is-the-proper-way-to-construct-cartesianindices-from-cartesianindex-in-julia-1-0/15579)._
