# Using \`CartesianIndex\` with Flux.jl

**URL:** <https://discourse.julialang.org/t/using-cartesianindex-with-flux-jl/134279>\
**Category:** General Usage\
**Tags:** question, flux, zygote\
**Created:** [December 2, 2025, 8:35am UTC](https://discourse.julialang.org/t/using-cartesianindex-with-flux-jl/134279 "2025-12-02T08:35:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chrysoberyl](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@Chrysoberyl](https://discourse.julialang.org/u/Chrysoberyl)\
**Post date:** [December 2, 2025, 8:35am UTC](https://discourse.julialang.org/t/using-cartesianindex-with-flux-jl/134279/1 "2025-12-02T08:35:41Z")

</div>

Is it possible to use `CartesianIndex` with `Flux.jl` within a `Flux.with_gradient` block?

Specifically I have `a::Vector{Float32}` and `b::Vector{CartesianIndex}`, and `a[b]` is in the gradient block. When I used this I got

```julia-auto
ERROR: LoadError: ArgumentError: unable to check bounds for indices of type CartesianIndex{2}
Stacktrace:
  [1] checkindex(::Type{Bool}, inds::Base.OneTo{Int64}, i::CartesianIndex{2})
    @ Base ./abstractarray.jl:751
  [2] checkindex
    @ ./abstractarray.jl:767 [inlined]
  [3] checkbounds
    @ ./abstractarray.jl:689 [inlined]
  [4] checkbounds
    @ ./abstractarray.jl:699 [inlined]
  [5] _getindex
    @ ./multidimensional.jl:955 [inlined]
  [6] getindex
    @ ./abstractarray.jl:1342 [inlined]
  [7] rrule
    @ ~/.julia/packages/ChainRules/14CDN/src/rulesets/Base/indexing.jl:63 [inlined]
  [8] rrule
    @ ~/.julia/packages/ChainRulesCore/Vsbj9/src/rules.jl:138 [inlined]
  [9] chain_rrule
    @ ~/.julia/packages/Zygote/55SqB/src/compiler/chainrules.jl:234 [inlined]
 [10] macro expansion
    @ ~/.julia/packages/Zygote/55SqB/src/compiler/interface2.jl:-1 [inlined]
 [11] _pullback(::Zygote.Context{false}, ::typeof(getindex), ::Matrix{Float16}, ::Vector{CartesianIndex})
    @ Zygote ~/.julia/packages/Zygote/55SqB/src/compiler/interface2.jl:81

```

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [December 2, 2025, 5:54pm UTC](https://discourse.julialang.org/t/using-cartesianindex-with-flux-jl/134279/2 "2025-12-02T17:54:24Z")

</div>

From the stack trace it looks like you’re doing this, `getindex( ::Matrix{Float16}, ::Vector{CartesianIndex})`, which doesn’t immediately fail:

```julia-auto
julia> let a = randn(3,3)
        b = [CartesianIndex(1,1), CartesianIndex(2,2)]
       gradient(x -> sum(x[b]), a)
       end
([1.0 0.0 0.0; 0.0 1.0 0.0; 0.0 0.0 0.0],)

```

Can you reduce your case to a MWE which keeps the failure?

---

<div class="post-metadata">

**Author:** ![Chrysoberyl](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@Chrysoberyl](https://discourse.julialang.org/u/Chrysoberyl)\
**Post date:** [December 3, 2025, 2:29am UTC](https://discourse.julialang.org/t/using-cartesianindex-with-flux-jl/134279/3 "2025-12-03T02:29:19Z")

</div>

This example reproduces the error:

```julia
using Flux, Statistics

function to_cartesian_mask(is::Vector{Int})::Vector{CartesianIndex}
    [CartesianIndex(j, i) for (i, j) in enumerate(is)]
end

a = randn(3, 3)
mask = BitVector([1,0,1])
is = [2, 1]
loss, grad = Flux.withgradient(a) do a
    #js = [CartesianIndex(j, i) for (i, j) in enumerate(is)]
    #z = a[:, mask][js]
    z = a[:, mask][to_cartesian_mask(is)]
    mean(z .^ 2)
end

```

I think the problem is with this `to_cartesian_mask` function call, because if I replace that with the inlined version, the error goes away.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 3, 2025, 3:49am UTC](https://discourse.julialang.org/t/using-cartesianindex-with-flux-jl/134279/4 "2025-12-03T03:49:15Z")

</div>

> [@Chrysoberyl](#):
>
> ```julia-auto
> function to_cartesian_mask(is::Vector{Int})::Vector{CartesianIndex}
> [CartesianIndex(j, i) for (i, j) in enumerate(is)]
> end
> 
> ```

The return type specification `::Vector{CartesianIndex}` is usually redundant and in this case detrimental, as it overrides the concrete type `Vector{CartesianIndex{2}}`.  
When I removed the return type, and left:

```julia-auto
function to_cartesian_mask(is::Vector{Int})
    [CartesianIndex(j, i) for (i, j) in enumerate(is)]
end

```

seemed to work better.
