# Creating CartesianIndex

**URL:** <https://discourse.julialang.org/t/creating-cartesianindex/70721>\
**Category:** New to Julia\
**Tags:** question, cartesianindices\
**Created:** [November 1, 2021, 8:36am UTC](https://discourse.julialang.org/t/creating-cartesianindex/70721 "2021-11-01T08:36:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sransome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sransome/32/29810_2.png) [@sransome](https://discourse.julialang.org/u/sransome)\
**Post date:** [November 1, 2021, 8:36am UTC](https://discourse.julialang.org/t/creating-cartesianindex/70721/1 "2021-11-01T08:36:51Z")

</div>

Hi,

I’m creating cartesianindexes for an 8 dimensional matrix using something like  
`mypoint = indexin(1.0f0,mybigmatrix)`

This works fine and gives me something like …  
‘’‘0-dimensional Array{Union{Nothing, CartesianIndex{8}}, 0}:  
CartesianIndex(1, 1, 1, 1, 1, 1, 1, 1)’‘’

However, now I would like to create a new CartesianIndex by incrementing a dimension i , so I can add it to a vector of CartesianIndexes.  
something like

`newpoint = myincrementingfunction (mypoint[1],i) #i is the dimension of the index I wish to increment`

and I expect the function to return CartesianIndex(1, 1, 2, 1, 1, 1, 1, 1) for i=3

It seems CartesianIndexes are imutable and I can’t see a way of converting them to a vector, so I can change the index and that’s not a lot of point because I don’t know how to convert a Vector back to a CartesianIndex anyway and converting to a Tuple doesn’t lead to a solution either,

Can anyone suggest the best way to achieve this please.

Thanks  
Steve

---

<div class="post-metadata">

**Author:** ![jipolanco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jipolanco/32/12129_2.png) [@jipolanco](https://discourse.julialang.org/u/jipolanco)\
**Post date:** [November 1, 2021, 8:42am UTC](https://discourse.julialang.org/t/creating-cartesianindex/70721/2 "2021-11-01T08:42:22Z")

</div>

You could use `Base.setindex`:

```julia
julia> I = CartesianIndex(1, 1, 1, 1, 1, 1, 1, 1)
CartesianIndex(1, 1, 1, 1, 1, 1, 1, 1)

julia> Base.setindex(I, I[3] + 1, 3)
CartesianIndex(1, 1, 2, 1, 1, 1, 1, 1)

```

---

<div class="post-metadata">

**Author:** ![sransome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sransome/32/29810_2.png) [@sransome](https://discourse.julialang.org/u/sransome)\
**Post date:** [November 1, 2021, 8:57am UTC](https://discourse.julialang.org/t/creating-cartesianindex/70721/3 "2021-11-01T08:57:32Z")

</div>

> [@jipolanco](#):
>
> `Base.setindex(I, I[3] + 1, 3)`

thanks Juan, that’s perfect.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 2, 2021, 4:56pm UTC](https://discourse.julialang.org/t/creating-cartesianindex/70721/4 "2021-11-02T16:56:59Z")

</div>

Another suggestion:

```julia
n = 8
I = CartesianIndex(ones(Int, n)...)
U = ntuple(i -> CartesianIndex((1:n .== i)...), n)

I + U[3] # increment 3rd index of I by 1
I + 3U[5] # increment 5th index of I by 3

```
