# How to increment CartesianIndex?

**URL:** https://discourse.julialang.org/t/how-to-increment-cartesianindex/111794
**Category:** New to Julia
**Tags:** cartesianindices
**Created:** [March 18, 2024, 10:10pm UTC](https://discourse.julialang.org/t/how-to-increment-cartesianindex/111794 "2024-03-18T22:10:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [March 18, 2024, 10:10pm UTC](https://discourse.julialang.org/t/how-to-increment-cartesianindex/111794/1 "2024-03-18T22:10:00Z")

</div>

Hello!

I think I am just not finding the right command, but how to increment a CartesianIndex? I have a matrix of size 5x5. Suppose I have a cartesian index of `CartesianIndex(3,1)` how to increment it to `CartesianIndex(4,1)` based on the information of matrix size?

Kind regards

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [March 18, 2024, 10:20pm UTC](https://discourse.julialang.org/t/how-to-increment-cartesianindex/111794/2 "2024-03-18T22:20:06Z")

</div>

```julia-repl
julia> CartesianIndex(3,1)+CartesianIndex(1,0)
CartesianIndex(4, 1)

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 18, 2024, 10:33pm UTC](https://discourse.julialang.org/t/how-to-increment-cartesianindex/111794/3 "2024-03-18T22:33:03Z")

</div>

Depends on what you mean by “increment”, but you can also use `nextind` to get the next (consecutive) index in a given array:

```julia
julia> A = rand(5,5);

julia> nextind(A, CartesianIndex(3,1))
CartesianIndex(4, 1)

```

(Better documentation for this was recently merged: [Document the generic functions nextind() and prevind() by JamesWrigley · Pull Request #52658 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/52658))

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [March 18, 2024, 10:35pm UTC](https://discourse.julialang.org/t/how-to-increment-cartesianindex/111794/4 "2024-03-18T22:35:37Z")

</div>

That is extremely close to what I want. I notice that it does not error out though:

```julia
julia> A = zeros(5,5)
5×5 Matrix{Float64}:
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0

julia> nextind(A, CartesianIndex(4,1))
CartesianIndex(5, 1)

julia> nextind(A, CartesianIndex(4,1))
CartesianIndex(5, 1)

julia> nextind(A, CartesianIndex(5,5))
CartesianIndex(1, 6)

julia> nextind(A, CartesianIndex(5,6))
CartesianIndex(1, 7)

julia> nextind(A, CartesianIndex(5,7))
CartesianIndex(1, 8)

```

I would like it to throw `nothing` or return the same value when doing 5,5 since it is last element.

Kind regards

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 18, 2024, 10:39pm UTC](https://discourse.julialang.org/t/how-to-increment-cartesianindex/111794/5 "2024-03-18T22:39:46Z")

</div>

> [@Ahmed\_Salih](#):
>
> I would like it to throw `nothing` or return the same value when doing 5,5 since it is last element.

Then you can use `iterate` with `CartesianIndices(A)`:

```julia
julia> I = CartesianIndices(A)
CartesianIndices((5, 5))

julia> iterate(I, CartesianIndex(4,1))
(CartesianIndex(5, 1), CartesianIndex(5, 1))

julia> iterate(I, CartesianIndex(5,4))
(CartesianIndex(1, 5), CartesianIndex(1, 5))

julia> iterate(I, CartesianIndex(4,5))
(CartesianIndex(5, 5), CartesianIndex(5, 5))

julia> iterate(I, CartesianIndex(5,5)) # last index, returns nothing

```

(When it doesn’t return `nothing`, it returns the same index twice due to the `iterate` API, but you can just ignore the second return value and the compiler should eliminate it.)
