# Creating CartesianIndex

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

You could use `Base.setindex`:

``````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)
``````
2 Likes

thanks Juan, that’s perfect.

Another suggestion:

``````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
``````
1 Like