Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I load a csv file with complex numbers in julia?

Tags:

csv

julia

I am trying to access some complex numbers I wrote to a csv file in julia but I'm having trouble getting it to recognize them. To understand what is happening, consider the following

a = [1+2.3im, 2.3+0im]
writecsv("test.csv",a)
b = readcsv("test.csv")

Now, if I interrogate the types

julia> typeof(b)
Array{Any,2}

julia> typeof(a)
Array{Complex{Float64},1}

And I cannot use the elements of b as complex numbers, just as a string.(b[1] is "1.0 + 2.3im", for instance).

like image 697
qgp07 Avatar asked May 05 '15 22:05

qgp07


1 Answers

Here's one way:

julia> b = map(x->eval(parse(x)),readcsv("test.csv"))
2x1 Array{Complex{Float64},2}:
 1.0+2.3im
 2.3+0.0im
like image 109
rickhg12hs Avatar answered Nov 15 '22 21:11

rickhg12hs