Michael Ohlrogge
Michael Ohlrogge

Reputation: 10980

Concatenate array of arrays

I have an array of arrays in Julia and am trying to find a way to concatenate all of the elements together. If I create the arrays and feed them each individually into hcat(), it performs exactly as I would want. But, if I create the arrays and then feed the array of arrays into hcat(), it fails. I could just write a loop to successfully concatenate one array to another, but I am wondering if there is a better way.

a = ones(2,2);
b = ones(2,2);
c = ones(2,2);    

hcat(a,b,c) ## Does what I want by creating a single array.  would be impracticable though for large number of objects.  

d = Array(Array{Float64,2}, 3);
d[1] = a;
d[2] = b;
d[3] = c;    

hcat(d) ## Still leaves me with an array of arrays, like before    

[a b c]  ## also does what I want
[f for f in d]  ## Still leaves me with an array of arrays

Upvotes: 2

Views: 1058

Answers (1)

Reza Afzalan
Reza Afzalan

Reputation: 5746

julia> hcat(d)
3x1 Array{Array{Float64,2},2}:
 2x2 Array{Float64,2}:
 1.0  1.0
 1.0  1.0
 2x2 Array{Float64,2}:
 1.0  1.0
 1.0  1.0
 2x2 Array{Float64,2}:
 1.0  1.0
 1.0  1.0

julia> hcat(d...)
2x6 Array{Float64,2}:
 1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0

Upvotes: 9

Related Questions