Can OffsetArrays be concatenated without losing the offsets?

Here is what I am trying to do:

julia> y = OffsetArray{ComplexF64}(undef,-1:1,1:2);

julia> axes(cat(y,y,dims=2))
(Base.OneTo(3), Base.OneTo(4))

julia> typeof(cat(y,y,dims=2))
Array{Complex{Float64},2}

In this example I lose the offset along the first dimension as the result is a normal array with 1-based indexing. Is there any way to obtain the result as an offset array without preallocating the result? On a related note, can we convert an ordinary array to an Offset Array by adding the information about its indices?

I would just wrap the result in an OffsetArray, since that is a very cheap operation.

methods(OffsetArrays)

shows you all the constructors, eg

julia> OffsetArray(ones(3), -1:1)
OffsetArray(::Array{Float64,1}, -1:1) with eltype Float64 with indices -1:1:
 1.0
 1.0
 1.0
1 Like

There’s also https://github.com/JuliaArrays/CatIndices.jl. The concatenation is very incomplete and not even described in the README, but you can learn a bit about what’s implemented by looking at the tests: https://github.com/JuliaArrays/CatIndices.jl/blob/master/test/runtests.jl. I’ve never needed more than what’s there, which is why it’s not complete, but PRs to round out the functionality and documentation would be welcome.

1 Like