How to convert from Array{Float64,2} to Array{Array{Float64,1},1} in Julia

Viewed 650

So, I have been working with a dataset (of my own creation) which was name as mydata and it was a 10000-element Array{Array{Float64,1},1}:. The thing is that I stored it into my computer by using writedlm("mydata",mydata) and now, when I read it using readdlm("mydata",mydata), I recover a matrix of type Array{Float64,2}:.

The question is:

  • Is there an easy way to read the data as a 10000-element Array{Array{Float64,1},1}:, or, at least, transform my recovered matrix to a Array{Array{Float64,1},1}: type?

Thanks!

3 Answers

You can drop the dimension using [:] operator:

julia> a=rand(2,3)
2×3 Array{Float64,2}:
 0.896784  0.653296  0.939789
 0.113942  0.178925  0.470658


julia> @view a[:]
6-element view(::Array{Float64,1}, :) with eltype Float64:
 0.8967838440135203
 0.1139418024781611
 0.6532956345656487
 0.17892503362478984
 0.9397886609896129
 0.4706578162765451

The advantage of the above code is that no data is copied so this is practically a zero cost.

There is also a reshape(a,6) (or more generally, reshape(a, length(a))) option which does not copy the data neither (you still get reference to the same memory location).

Finally, note that you might want to use Array slicing operator to actually be sure that you are getting only the first columns (again @view avoids data copying):


julia> @view a[:,1]
2-element view(::Array{Float64,2}, :, 1) with eltype Float64:
 0.8967838440135203
 0.1139418024781611

Apart from patterns shown by Przemyslaw, I also often use:

julia> a=rand(2,3)
2×3 Array{Float64,2}:
 0.946128  0.902697  0.831427
 0.647748  0.934436  0.221258

julia> vec(a)
6-element Array{Float64,1}:
 0.9461283739840134
 0.6477484162303082
 0.9026969223072401
 0.934435667665475
 0.8314267308266441
 0.22125755489262455

The result of vec(a) also shares data with a (so there is no copying) and it can be slightly faster to construct and use later than @view a[:] as it is a plain vector rather than a view.

To get a vector of vectors, you can write

collect(eachcol(a)) 

Edit: In order to get an ordinary Array{Array{Float64,1},1}, you can use

collect.(eachcol(a))
Related