JuliaArrays / OffsetArrays.jl

Fortran-like arrays with arbitrary, zero or negative starting indices.
Other
195 stars 40 forks source link

Results of concatenating an offset array #326

Closed KeithWM closed 1 year ago

KeithWM commented 1 year ago

Hi there,

I'm using OffsetArrays in image manipulation, and I've come across something where the behaviour of an OffsetArray is a bit disappointing. When concatenating a vector to the end (or beginning) of an offset matrix, I expect this to still return an OffsetArray.

oa = OffsetArrays.OffsetArray([1 2; 3 4], -1:0, 3:4)
hcat(oa, oa[:, begin]) |> axes  # (Base.OneTo(2), Base.OneTo(3))

I think I can get the desired behaviour by implementing

function Base.hcat(
    mat::OffsetMatrix{T, Matrix{T}}, vec::OffsetVector{T, Vector{T}}
) where {T}
    ax = axes(mat, 2)
    new_ax = first(ax):(last(ax)+1)
    content = hcat(mat.parent, vec)
    result = OffsetMatrix{T, Matrix{T}}(content, axes(mat, 1), new_ax)
end
hcat(oa, oa[:, begin]) |> axes  # (IdOffsetRange(values=-1:0, indices=-1:0), IdOffsetRange(values=3:5, indices=3:5))

Is this something worth implementing? It would also have to be done for hcat(vec, mat) and both vcat options. What do you think?

jishnub commented 1 year ago

See https://github.com/JuliaLang/julia/pull/37629

KeithWM commented 1 year ago

Thanks. Obviously I did a poor job of researching whether this issue was already known. My apologies. Keep up the good work all!