Filter CartesianIndices

I am using CartesianIndex as coordinates for my simulation lattice grid.

Since steps are not allow in the constructor of CartesianIndices, is there an efficient way to generate iterators of CartesianIndex’s that pick up only

CartesianIndex(1,1,1), CartesianIndex(3,1,1), CartesianIndex(1,3,1),CartesianIndex(1,1,3), CartesianIndex(3,3,1), CartesianIndex(3,1,3), CartesianIndex(1,3,3), CartesianIndex(3,3,3)

from CartesianIndices(4,4,4)?

You can just index into it (or even use a view):

``````julia> CartesianIndices((4,4,4))[1:2:3, 1:2:3, 1:2:3]
2×2×2 Array{CartesianIndex{3},3}:
[:, :, 1] =
CartesianIndex(1, 1, 1)  CartesianIndex(1, 3, 1)
CartesianIndex(3, 1, 1)  CartesianIndex(3, 3, 1)

[:, :, 2] =
CartesianIndex(1, 1, 3)  CartesianIndex(1, 3, 3)
CartesianIndex(3, 1, 3)  CartesianIndex(3, 3, 3)
``````

You can also sometimes get the computation of the CartesianIndex to fuse with other portions of your calculation with broadcasting:

``````julia> CartesianIndex.(1:2:3, reshape(1:2:3, 1, :), reshape(1:2:3, 1, 1, :))
2×2×2 Array{CartesianIndex{3},3}:
[:, :, 1] =
CartesianIndex(1, 1, 1)  CartesianIndex(1, 3, 1)
CartesianIndex(3, 1, 1)  CartesianIndex(3, 3, 1)

[:, :, 2] =
CartesianIndex(1, 1, 3)  CartesianIndex(1, 3, 3)
CartesianIndex(3, 1, 3)  CartesianIndex(3, 3, 3)
``````

What will be most efficient will all depend upon how often you need to create this thing and how you’re using it.