# Strange behaviour using hcat with arrays obtained from arraysplit

**URL:** <https://discourse.julialang.org/t/strange-behaviour-using-hcat-with-arrays-obtained-from-arraysplit/35685>\
**Category:** New to Julia\
**Created:** [March 7, 2020, 11:17am UTC](https://discourse.julialang.org/t/strange-behaviour-using-hcat-with-arrays-obtained-from-arraysplit/35685 "2020-03-07T11:17:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ceysa75](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ceysa75/32/19121_2.png) [@ceysa75](https://discourse.julialang.org/u/ceysa75)\
**Post date:** [March 7, 2020, 11:17am UTC](https://discourse.julialang.org/t/strange-behaviour-using-hcat-with-arrays-obtained-from-arraysplit/35685/1 "2020-03-07T11:17:12Z")

</div>

Hi all,

I experience a strange behaviour using hcat with arrays obtained from arraysplit (DSP package). My issue is explained in the code snippet below:

```julia
using DSP

a = 1:10
q = arraysplit(a,5,3)

A = hcat(q[1],q[2]) # Leads to 2 identical columns ??

```

For some reason A results in

```julia
julia> A
5×2 Array{Float64,2}:
 3.0 3.0
 4.0 4.0
 5.0 5.0
 6.0 6.0
 7.0 7.0

```

However, I would have expected A becomes as follows

```julia
julia> A
5×2 Array{Float64,2}:
 1.0 3.0
 2.0 4.0
 3.0 5.0
 4.0 6.0
 5.0 7.0

```

What am I misunderstanding here ?

Regards

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 7, 2020, 11:47am UTC](https://discourse.julialang.org/t/strange-behaviour-using-hcat-with-arrays-obtained-from-arraysplit/35685/2 "2020-03-07T11:47:58Z")

</div>

Yeah I’ve noticed that as well, it’s probably due to the implementation of arraysplit to minimize allocations. You may use `copy` of the splits to get what you want.

[https://github.com/JuliaDSP/DSP.jl/blob/8edec5c75a9461869c324ff6db4eefd7fd789a35/src/periodograms.jl#L42](https://github.com/JuliaDSP/DSP.jl/blob/8edec5c75a9461869c324ff6db4eefd7fd789a35/src/periodograms.jl#L42)  
Here you see how it returns the same array each iteration, it just has different values in it. Probably because views allocate?

---

<div class="post-metadata">

**Author:** ![ceysa75](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ceysa75/32/19121_2.png) [@ceysa75](https://discourse.julialang.org/u/ceysa75)\
**Post date:** [March 7, 2020, 1:16pm UTC](https://discourse.julialang.org/t/strange-behaviour-using-hcat-with-arrays-obtained-from-arraysplit/35685/3 "2020-03-07T13:16:06Z")

</div>

Ok. Thank you for the hint. It works with

```julia
A = hcat(collect(q[1]),collect(q[2]))

```

Could you help me with another issue related to that? What if I want to concatenate many more arrays as given in the example below.

```julia
using DSP
a = 1:50
q = arraysplit(a,5,3)

A = hcat(collect(q[1]),collect(q[2]) ... collect(q[23])) #how do I achieve that with an e.g. for loop ?

```

Thank you again for your help…

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 7, 2020, 1:22pm UTC](https://discourse.julialang.org/t/strange-behaviour-using-hcat-with-arrays-obtained-from-arraysplit/35685/4 "2020-03-07T13:22:54Z")

</div>

You can try `collect(copy.(arraysplit))`. I’m not by a computer so can not try it out.

Or potentially `reduce(hcat, copy.(arraysplit))`

---

<div class="post-metadata">

**Author:** ![ceysa75](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ceysa75/32/19121_2.png) [@ceysa75](https://discourse.julialang.org/u/ceysa75)\
**Post date:** [March 7, 2020, 2:03pm UTC](https://discourse.julialang.org/t/strange-behaviour-using-hcat-with-arrays-obtained-from-arraysplit/35685/5 "2020-03-07T14:03:24Z")

</div>

Thanks again. Gave up trying it with hcat. Solved it now as follows that gives me exactly what I need.

```julia
a = 1:10

q = arraysplit(a,5,3)
A = zeros(Float64,5,length(q))

for i = 1:length(q)
    A[:,i] = q[i]
end

```
