# How to use CartesianIndices to replace two nested loops

**URL:** <https://discourse.julialang.org/t/how-to-use-cartesianindices-to-replace-two-nested-loops/72034>\
**Category:** General Usage\
**Tags:** question, cartesianindices\
**Created:** [November 24, 2021, 10:23pm UTC](https://discourse.julialang.org/t/how-to-use-cartesianindices-to-replace-two-nested-loops/72034 "2021-11-24T22:23:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Post date:** [November 24, 2021, 10:23pm UTC](https://discourse.julialang.org/t/how-to-use-cartesianindices-to-replace-two-nested-loops/72034/1 "2021-11-24T22:23:46Z")

</div>

I’m trying to make a scatter plot where the x,y pairs form a (not necessarily rectangular) grid. The color of each point depends in a rather complicated way on the index of the point. Given two vectors, a1 and a2, my points are `n*a1+m*a2`, where n and m each span some integer range.

I made this work by using two nested loops and computing a single index for each point, but I can’t help but think that `CartesianIndices` is perfect for this use case. Despite spending a couple of hours, I couldn’t figure out how to use `CartesianIndices`. Here’s a minimal working example.

```julia
using Plots
nx, ny = 4,5
x = zeros(nx*ny)
y = zeros(nx*ny)
c = zeros(nx*ny)
a1, a2 = [1.1,.5], [-.2,1.0]
b = hcat(a1,a2)
for i in 0:nx-1
    for j in 0:ny-1
        idx = j+i*ny+1
        x[idx],y[idx] = b*[i,j]
        c[idx] = mod(i,7)/7+mod(j,3)/3
    end
end
scatter(x,y,marker_z=c,colormap=:thermal)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/f/bf19a0265742e01211d1d73b4e7910d877eb66ce.png)

While this is a passable solution, it seems rather like I’m still writing in Fortran…any suggestions on how to make this more Julian?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 24, 2021, 11:24pm UTC](https://discourse.julialang.org/t/how-to-use-cartesianindices-to-replace-two-nested-loops/72034/2 "2021-11-24T23:24:34Z")

</div>

One way:

```julia
CI = CartesianIndices((0:ny-1, 0:nx-1))
LI = LinearIndices(CI)
for (I, L) in zip(CI, LI)
    x[L], y[L] = b * [I[2], I[1]]
    c[L] = mod(I[2], 7)/7 + mod(I[1], 3)/3
end

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [November 25, 2021, 5:28am UTC](https://discourse.julialang.org/t/how-to-use-cartesianindices-to-replace-two-nested-loops/72034/3 "2021-11-25T05:28:12Z")

</div>

Do you really need CartesianIndices? A simple solution without them:

```julia
using StructArrays

xyc = map(Iterators.product(1:nx, 1:ny)) do (i, j)
    x, y = b * [i, j]
    c = mod(i,7)/7+mod(j,3)/3
    (; x, y, c)
end |> StructArray

# access x, y, c arrays as xyc.x and so on

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 25, 2021, 7:33am UTC](https://discourse.julialang.org/t/how-to-use-cartesianindices-to-replace-two-nested-loops/72034/4 "2021-11-25T07:33:11Z")

</div>

Without any additional packages one could also write this problem as a comprehension:

```julia
xyc = vec([[b*[i,j]..., mod(i,7)/7+mod(j,3)/3] for i in 0:nx-1, j in 0:ny-1])
x, y, c = [getindex.(xyc, i) for i in 1:3]

```

It should be possible to write this even simpler.

OP’s nested loops could also be written using the simplified syntax:

```julia
for i in 0:nx-1, j in 0:ny-1
..
end

```

---

<div class="post-metadata">

**Author:** ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Post date:** [November 26, 2021, 6:12pm UTC](https://discourse.julialang.org/t/how-to-use-cartesianindices-to-replace-two-nested-loops/72034/5 "2021-11-26T18:12:58Z")

</div>

I learned a lot looking at your solution. I haven’t used Iterators or StructArrays before so this really helpful in expanding my Julia repertoire. I also like that you didn’t need to pre-allocate the arrays. Thanks for the suggestions.

---

<div class="post-metadata">

**Author:** ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Post date:** [November 26, 2021, 6:14pm UTC](https://discourse.julialang.org/t/how-to-use-cartesianindices-to-replace-two-nested-loops/72034/6 "2021-11-26T18:14:44Z")

</div>

This answered my question directly. But your later suggestion, and @aplavin’s, are even better suggestions (I think). I like that neither one needs preallocated arrays.
