Int vector to CarthesianIndex

Dear all,

I would like to ask whether there is some way, how to transform a vector of Int64s to CarthesianIndex object.

Naive approach:

r = collect(1:5)
CartesianIndex( r )

failed me, and I don’t know how to proceed further. Any advice/suggestion would be welcome!

Best,
Honza

I’m assuming you want the cartesian index to look like CartesianIndex(1,2,3,4,5). You can construct that with a tuple rather than a vector:

julia> CartesianIndex(Tuple(1:5))
CartesianIndex(1, 2, 3, 4, 5)
3 Likes

Thank you, I found a workaround based on your suggestion.

Firstly transform Array into Tuple, and then to CartesianIndex.

rr = [1 5 6 7]
aa = Tuple(Int64(x) for x in rr)
tt = CartesianIndex(aa)

You can also use

r = collect(1:5)
CartesianIndex(r...)

Of course you don’t need the collect either, but I presume you’re converting more general arrays than what can be expressed by a UnitRange.

4 Likes