# Cartesian Indices Sequence on the GPU

**URL:** <https://discourse.julialang.org/t/cartesian-indices-sequence-on-the-gpu/134998>\
**Category:** General Usage\
**Tags:** gpu, cuda, cartesianindices\
**Created:** [January 12, 2026, 8:24am UTC](https://discourse.julialang.org/t/cartesian-indices-sequence-on-the-gpu/134998 "2026-01-12T08:24:05Z")\
**Posts on this page:** 1\
**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:** [January 12, 2026, 8:24am UTC](https://discourse.julialang.org/t/cartesian-indices-sequence-on-the-gpu/134998/1 "2026-01-12T08:24:05Z")

</div>

I have an array of integers `i`, e.g. `[3,1,2]`, and I want to map them to Cartesian indices `[(1, 3), (2, 1), (3, 2)]`.

On the CPU, the easy way of creating this array is `CartesianIndex.(enumerate(i))`. However this does not work on the GPU with CUDA arrays, because “Scalar indexing is disallowed”.

Other than converting the array back and forth between CPU and GPU, is there any way to map a GPU array `i` to a Cartesian index list? I found a solution:

```julia
to_ordered_index(i) = CartesianIndex.(1:length(i), i)

```

but I don’t know if this is optimal.

In my use case the index `i` could contain zeros that are then filtered out, for example `[3,0,1,2,0]` would be filtered to `[3,1,2]`:

```juia
mask = i .=! 0
oi = to_ordered_index(i[mask])

```

It is very important that invalid Cartesian indices like `(2,0)` do not show up anywhere since they could cause errors during backpropagation.
