Reading file in 1st array in Julia

I have a pretty simple text file with only numbers that looks like 0 1.57 3.14 This example has 3 numbers, but the sum is fine. I am trying to read them into the 1st Float 64 array, so I tried the following.

function read_x_data(fname)
  f=open(fname)
  xarr=readdlm(f, Float64)
  print(xarr)
  xarr=sortperm(xarr)   
end

However, I get an error that sortperm does not have an appropriate sortperm method (:: Array {Float64, 2}). I don’t understand why this is happening - how can I read my data into the 1st array? I saw a similar question in Reading line by line in Julia , but I find that using push n times like this is very inefficient, isn't it? Any help with my problem or suggestions is greatly appreciated. Thank you

+4
source share
2 answers

To answer your question: vecwill change any array to a 1d vector.

sortpermreturns a permutation, but not the original data; so your example, even if you added vec(xarr), throws the data. You probably want to sort.

Finally, Julia is push!not ineffective. You might expect it to be inefficient from experience with another language (Matlab?), But in Julia you can grow 1d arrays efficiently.

+2
source

If you intend to sort the data, this might work for you.

read_x_data(fname) = sort!(vec(readdlm(fname,Float64)))
+2
source

All Articles