Strange behaviour using hcat with arrays obtained from arraysplit

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:

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

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.


Here you see how it returns the same array each iteration, it just has different values in it. Probably because views allocate?

Ok. Thank you for the hint. It works with

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.

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…

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

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

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

a = 1:10

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

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