4
votes

I have an array of type Array{Float64,2} but it's an array of 1 column, and I'm unable to pass this into a function that's expecting a single-column array of type Array{Float64,1}. I don't really understand what the 2 means or how to fix my problem, and I haven't been able to figure it out by searching through any documentation.

1

1 Answers

5
votes

In Array{Float64,2}, the 2 is the number of dimensions in the array. Since you say it's "it's an array of 1 column", you probably have something which is 2-dimensional with either one row or one column, i.e. one of

julia> c = rand(1,3)
1x3 Array{Float64,2}:
 0.190944  0.928697  0.251519

julia> d = rand(3,1)
3x1 Array{Float64,2}:
 0.0818493
 0.0342291
 0.58341  

To turn this into a 1-dimensional array, you could slice the array manually or use squeeze, as you prefer:

julia> c[1,:]
3-element Array{Float64,1}:
 0.190944
 0.928697
 0.251519

julia> squeeze(d,2)
3-element Array{Float64,1}:
 0.0818493
 0.0342291
 0.58341  

Either approach should give you something of type Array{Float64,1}.


As noted in the comments, another approach is to use reshape, e.g. (using a different random c):

julia> reshape(c, length(c))
3-element Array{Float64,1}:
 0.680653 
 0.0573147
 0.607054 

This has the advantage -- and disadvantage -- of not caring whether you have an array of shape 1xN or Nx1.