What is the proper way to construct CartesianIndices from CartesianIndex in Julia 1.0?

isn’t it better to use CartesianIndex all the way down? Transforming to UnitRange make the code not quite clean.

Some things turn out to be easier with the range representation, and because ranges are sometimes used to encode the array type for unconventional indexing the new representation is the only truly correct approach because it allows you to preserve the type of ranges.

However, the lack of a good default for construction from CartesianIndex is an oversight that should be corrected (hopefully in julia 1.1, see Add colon constructor for CartesianIndices by timholy · Pull Request #29440 · JuliaLang/julia · GitHub). (See also PSA: replacement of ind2sub/sub2ind in Julia 0.7+ - #6 by mauro3). In the meantime I use this:

_colon(I::CartesianIndex{N}, J::CartesianIndex{N}) where N =
    CartesianIndices(map((i,j) -> i:j, Tuple(I), Tuple(J)))
1 Like