读入朱莉娅阵列 [英] read into arrays in Julia
问题描述
我是比较新的茱莉亚正在寻找一种有效的方式从文本文件中读取和存储阵列中的每个列(我有2列,但一般的解决方案将是巨大的为好)。举例来说,我想输入
1 2
3 4
5 6
要被读入两个阵列,比方说,x和y,使得x = [1 3 5]和y = [2 4 6]。我有一个工作的解决方案(可能无法编译,只是自由递到),但我觉得有这样做,而不是hcat和逐行读取输入文件行更有效的方式。任何建议都多少AP preciated!
目前,我做以下,或多或少:
X = [];
Y = [];
F =打开(文件名);
F = readlines方法(F);
对于F中的STR
S1,S2 =拆分(STR,);
S1 = INT(S1);
S2 = INT(S2);
X = hcat(X,S1);
Y = hcat(Y,S2);
结束
下面是一个办法。
朱莉娅> myArray的= INT(开(readdlmmynums.txt))
3x2的阵列{} Int32,2:
1 2
3 4
5 6朱莉娅> X = myArray的[:1]
3元阵{} Int32,1:
1
3
五朱莉娅> Y = myArray的[:2]
3元阵{} Int32,1:
2
4
6
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
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
Here's a way.
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
这篇关于读入朱莉娅阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!