# Filter CartesianIndices

**URL:** <https://discourse.julialang.org/t/filter-cartesianindices/23013>\
**Category:** General Usage\
**Tags:** indexing\
**Created:** [April 10, 2019, 9:41pm UTC](https://discourse.julialang.org/t/filter-cartesianindices/23013 "2019-04-10T21:41:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yingjerkao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yingjerkao/32/7658_2.png) [@yingjerkao](https://discourse.julialang.org/u/yingjerkao)\
**Post date:** [April 10, 2019, 9:41pm UTC](https://discourse.julialang.org/t/filter-cartesianindices/23013/1 "2019-04-10T21:41:09Z")

</div>

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

---

<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 10, 2019, 9:55pm UTC](https://discourse.julialang.org/t/filter-cartesianindices/23013/2 "2019-04-10T21:55:57Z")

</div>

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

```julia
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
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.
