在 Julia 中读入数组 [英] read into arrays in Julia
问题描述
我对 Julia 比较陌生,正在寻找一种从文本文件中读取数据并将每个列"存储在数组中的有效方法(我有 2 列,但一般的解决方案也很好).例如,我想要输入
I'm relatively new to Julia and am looking for an efficient way to read in from a text file and store each "column" in an array (I have 2 columns, but a general solution would be great as well). For instance, I'd like the input
1 2
3 4
5 6
被读入两个数组,例如 x 和 y,使得 x=[1 3 5] 和 y=[2 4 6].我有一个可行的解决方案(可能无法编译,只是随意),但我觉得有一种比 hcat 和逐行读取输入文件更有效的方法.任何建议都非常感谢!
to be read into two arrays, say, x and y, such that x=[1 3 5] and y=[2 4 6]. I have a working solution (might not compile, just free-handed it), but I feel like there is a more efficient way to do this than to hcat and to read the input file line by line. Any suggestions are much appreciated!
目前,我或多或少正在做以下事情:
Currently, I am doing the following, more or less:
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
推荐答案
这里有办法.
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
这篇关于在 Julia 中读入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!