# 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:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jwu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jwu/32/3775_2.png) [@jwu](https://discourse.julialang.org/u/jwu)
#### Post date: [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/1 "2018-09-27T21:00:47Z")

</div>

It is recommended to use unit ranges to construct CartesianIndices, but isn’t it better to use CartesianIndex all the way down? Transforming to UnitRange make the code not quite clean.

```julia
julia> c1 = CartesianIndex((2,2))                                                                     
CartesianIndex(2, 2)                                                                                                                                                         
                                                                                                      
julia> c2 = CartesianIndex((4,5))                                                                     
CartesianIndex(4, 5)                                                                                  
                                                                                                      
julia> CartesianIndices(c1,c2)                                                                        
┌ Warning: the internal representation of CartesianIndices has changed, use `CartesianIndices((2:4, 2:
5))` (or other more appropriate AbstractUnitRange type) instead.                                      
│ caller = top-level scope at none:0                                                                
└ @ Core none:0                                                                                       
3×4 CartesianIndices{2,Tuple{UnitRange{Int64},UnitRange{Int64}}}:                                     
 CartesianIndex(2, 2) CartesianIndex(2, 3) CartesianIndex(2, 4) CartesianIndex(2, 5)               
 CartesianIndex(3, 2) CartesianIndex(3, 3) CartesianIndex(3, 4) CartesianIndex(3, 5)               
 CartesianIndex(4, 2) CartesianIndex(4, 3) CartesianIndex(4, 4) CartesianIndex(4, 5)               

```

---

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

```

---

<div class="post-metadata">

### Author: ![jwu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jwu/32/3775_2.png) [@jwu](https://discourse.julialang.org/u/jwu)
#### Post date: [September 30, 2018, 2:30pm UTC](https://discourse.julialang.org/t/what-is-the-proper-way-to-construct-cartesianindices-from-cartesianindex-in-julia-1-0/15579/3 "2018-09-30T14:30:03Z")

</div>

thanks for the explanation.  
The transformation to Tuple has a little bit overhead and is unnecessary?

---

<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, 2:34pm UTC](https://discourse.julialang.org/t/what-is-the-proper-way-to-construct-cartesianindices-from-cartesianindex-in-julia-1-0/15579/4 "2018-09-30T14:34:30Z")

</div>

There’s no overhead: `Tuple(I)` just returns `I.I` (which is already a tuple):

```julia
julia> I = CartesianIndex(2,3)
CartesianIndex(2, 3)

julia> @code_lowered Tuple(I)
CodeInfo(                                                                                                                                                                                                                                                                      
95 1 ─ %1 = (Base.getproperty)(index, :I) │   
   └── return %1 │   
)                                                                                                                                                                                                                                                                                                                                                                                   

```

In general we discourage direct field access when there’s a functional form that makes perfect sense, but either is fine.

> and is unnecessary

Actually, it is necessary because `CartesianIndex` is deliberately not iterable. The original motivation for keeping them non-iterable was a performance problem due to limitations in inference (e.g., [Add `Tuple(::CartesianIndex)` by martinholters · Pull Request #23719 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/23719)), but even if we fix that @mbauman has correctly pointed out that expressions like `getindex.((A,), CartesianIndex((1,2)), [3,4,5])` would break if we made them iterable. So I think they’re going to stay non-iterable, and hence you need to convert them into tuples for use in `map`.

---

<div class="post-metadata">

### Author: ![jwu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jwu/32/3775_2.png) [@jwu](https://discourse.julialang.org/u/jwu)
#### Post date: [September 30, 2018, 2:52pm UTC](https://discourse.julialang.org/t/what-is-the-proper-way-to-construct-cartesianindices-from-cartesianindex-in-julia-1-0/15579/5 "2018-09-30T14:52:02Z")

</div>

thanks, I see you created a PR.  
[https://github.com/JuliaLang/julia/pull/29440](https://github.com/JuliaLang/julia/pull/29440)
