# Can OffsetArrays be concatenated without losing the offsets?

**URL:** <https://discourse.julialang.org/t/can-offsetarrays-be-concatenated-without-losing-the-offsets/16950>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 30, 2018, 6:46am UTC](https://discourse.julialang.org/t/can-offsetarrays-be-concatenated-without-losing-the-offsets/16950 "2018-10-30T06:46:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [October 30, 2018, 6:46am UTC](https://discourse.julialang.org/t/can-offsetarrays-be-concatenated-without-losing-the-offsets/16950/1 "2018-10-30T06:46:23Z")

</div>

Here is what I am trying to do:

```julia
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?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 30, 2018, 7:00am UTC](https://discourse.julialang.org/t/can-offsetarrays-be-concatenated-without-losing-the-offsets/16950/2 "2018-10-30T07:00:07Z")

</div>

> [@jishnub](#):
>
> 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.

```julia
methods(OffsetArrays)

```

shows you all the constructors, eg

```julia
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

```

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [October 30, 2018, 8:07am UTC](https://discourse.julialang.org/t/can-offsetarrays-be-concatenated-without-losing-the-offsets/16950/3 "2018-10-30T08:07:24Z")

</div>

There’s also [https://github.com/JuliaArrays/CatIndices.jl](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](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.
