读入朱莉娅的数组

时间:2014-06-18 21:26:50

标签: arrays input julia

我对Julia相对较新,我正在寻找一种有效的方式从文本文件读入并将每个“列”存储在一个数组中(我有2列,但一般的解决方案也会很棒)。例如,我想要输入

    1 2
    3 4
    5 6

被读入两个数组,比如x和y,这样x = [1 3 5]和y = [2 4 6]。我有一个工作的解决方案(可能不会编译,只是自由交配),但我觉得有一种更有效的方法来做到这一点,而不是hcat和逐行读取输入文件。任何建议都非常感谢!

目前,我或多或少地做了以下事情:

x=[]; 
y=[]; 
f=open("filename"); 
f=readlines(f); 
for str in f 
     s1, s2= split(str, " "); 
     s1=int(s1); 
     s2=int(s2); 
     x=hcat(x, s1); 
     y=hcat(y, s2);
end

1 个答案:

答案 0 :(得分:8)

这是一种方式。

julia> myarray=int(open(readdlm,"mynums.txt"))
3x2 Array{Int32,2}:
 1  2
 3  4
 5  6

julia> x=myarray[:,1]
3-element Array{Int32,1}:
 1
 3
 5

julia> y=myarray[:,2]
3-element Array{Int32,1}:
 2
 4
 6