# CartesianIndices syntax

**URL:** <https://discourse.julialang.org/t/cartesianindices-syntax/22970>\
**Category:** General Usage\
**Tags:** indexing\
**Created:** [April 9, 2019, 7:25pm UTC](https://discourse.julialang.org/t/cartesianindices-syntax/22970 "2019-04-09T19:25:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pier/32/7335_2.png) [@Pier](https://discourse.julialang.org/u/Pier)\
**Post date:** [April 9, 2019, 7:25pm UTC](https://discourse.julialang.org/t/cartesianindices-syntax/22970/1 "2019-04-09T19:25:55Z")

</div>

Imagine a 5x5 matrix. One can look at the entries in rows 2:3 and columns 2 and 5 with view(A,2:3,[2,5]).  
However CartesianIndices((2:3,[2,5])) is not admissible, although I believe that the meaning of it is clear.  
The reason why I am looking into this is to copy from one array into another one with 0 allocation, which I currently do with

```julia
function unsafe_fastcopyto!(dest::AbstractArray{T1,N1}, Rdest::CartesianIndices{N1},
                         src::AbstractArray{T2,N2}, Rsrc::CartesianIndices{N2}) where {T1,T2,N1,N2}
        src′ = Base.unalias(dest, src)
        for (Is, Id) in zip(Rsrc, Rdest)
            @inbounds dest[Id] = src′[Is]
        end
end

```

Any ideas?

---

<div class="post-metadata">

**Author:** ![jbrea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbrea/32/3879_2.png) [@jbrea](https://discourse.julialang.org/u/jbrea)\
**Post date:** [April 9, 2019, 8:15pm UTC](https://discourse.julialang.org/t/cartesianindices-syntax/22970/2 "2019-04-09T20:15:11Z")

</div>

Not super elegant, but would this

```julia
hcat((CartesianIndices((2:3, i)) for i in [2, 5])...)

```

work for you?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [April 9, 2019, 8:46pm UTC](https://discourse.julialang.org/t/cartesianindices-syntax/22970/3 "2019-04-09T20:46:27Z")

</div>

The key is that @Pier seems to be after peak performance — and reducing all allocations at all costs. Typically you wouldn’t need `CartesianIndices` or `CartesianIndex` at all. Just using regular old indexed assignment with a view would be enough.

```julia
dest[2:4, [2,5]] = @view src[2:4, [2,5]]

```

That will work and be reasonably fast… but it’ll allocate a small wrapper for the view and won’t be quite as fast as just writing out the loops manually.

Someone else just asked for this capability a day or two ago: [Support CartesianIndices with Arrays and other iterables, not just ranges (alternative?) - #8 by mbauman](https://discourse.julialang.org/t/support-cartesianindices-with-arrays-and-other-iterables-not-just-ranges-alternative/22812/8). I suppose we could keep the specializations for the all-UnitRange case and just allow it to behave like `map(CartesianIndex, Iterators.product(...))` in other cases.

---

<div class="post-metadata">

**Author:** ![Pier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pier/32/7335_2.png) [@Pier](https://discourse.julialang.org/u/Pier)\
**Post date:** [April 9, 2019, 8:50pm UTC](https://discourse.julialang.org/t/cartesianindices-syntax/22970/4 "2019-04-09T20:50:15Z")

</div>

@jbrea Thanks for the suggestion. Your approach actually produces the desired indices. Only drawback is that the syntax is not super tidy.

---

<div class="post-metadata">

**Author:** ![Pier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pier/32/7335_2.png) [@Pier](https://discourse.julialang.org/u/Pier)\
**Post date:** [April 9, 2019, 8:59pm UTC](https://discourse.julialang.org/t/cartesianindices-syntax/22970/5 "2019-04-09T20:59:18Z")

</div>

@mbauman in fact the key reason is to avoid the generation of the wrapper because the copy happens in a super hot triple nested loop (a total of the order of 1E8 - 1E9 passes). I also wish that CartesianIndices would allow more flexibility as discussed in the post you linked.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [April 9, 2019, 9:07pm UTC](https://discourse.julialang.org/t/cartesianindices-syntax/22970/6 "2019-04-09T21:07:53Z")

</div>

I mean, if you’re already writing your own `unsafe_fastcopyto!` function, I’d just hard-code the number of dimensions and just write out the for loops manually. IIRC this still beats iterating over `CartesianIndices` by a bit in the cases that CartesianIndices can handle.

In other words, just do:

```julia
function unsafe_fastcopyto!(dest::AbstractMatrix, Idest, Jdest,
                         src::AbstractMatrix,Isrc, Jsrc)
        src′ = Base.unalias(dest, src)
        for (Js, Jd) in zip(Jsrc, Jdest), (Is, Id) in zip(Isrc, Idest)
            @inbounds dest[Id, Jd] = src′[Is, Js]
        end
end

```

---

<div class="post-metadata">

**Author:** ![Pier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pier/32/7335_2.png) [@Pier](https://discourse.julialang.org/u/Pier)\
**Post date:** [April 9, 2019, 9:19pm UTC](https://discourse.julialang.org/t/cartesianindices-syntax/22970/7 "2019-04-09T21:19:26Z")

</div>

Sometimes dest is a matrix and sometimes a vector, this is why I was using the CartesianIndices. But you are right, I will specialize for the two cases.
