read into arrays in Julia - arrays

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

Related

Read an Array of Arrays separated by blank lines in Julia

I have an array of arrays stored as blocks of tabular data in a textfile. The blocks have different number of rows but the same number of columns. Like this:
7 9
9 9
7 1
1 1
3 3
4 1
And so on.
I would like to read them in Julia and to end with an array of arrays, or an array of 2 dimensional arrays, like this:
a=[ [7, 9; 9 ,9], [7, 1; 1, 1 ; 3 3 ]] ...
I am trying with different ideas with do syntax, but I am being not very succesfull yet.
aux=[]
open("cell.dat") do f
aux=[]
aux2=[]
for line in eachline(f)
if line != ""
aux2=vcat(aux2,line)
else
print("tuabuela")
aux=vcat(aux,aux2)
print(aux, aux2)
aux2=[]
end
end
end
I end with an empty array!
There are many ways to do it. Suppose the file is not huge and you have read your file to String called dat:
dat="""7 9
9 9
7 1
1 1
3 3
4 1"""
In that case you can do:
julia> readdlm.(IOBuffer.(split(dat,"\n\n")))
3-element Vector{Matrix{Float64}}:
[7.0 9.0; 9.0 9.0]
[7.0 1.0; 1.0 1.0; 3.0 3.0]
[4.0 1.0]

How to concatenate differently sized vectors in Julia?

How can I concatenate arrays of different size with a "filler" value where the arrays don't line up?
a = [1,2,3]
b = [1,2]
And I would like:
[1 2 3
1 2 missing]
Or
[1 2 3
1 2 nothing]
One way, using rstack which is "ragged stack". It always places arrays along one new dimension, thus given vectors, they form the columns of a matrix. (The original question may want the transpose of this result.)
julia> using LazyStack
julia> rstack(a, b; fill=missing)
3×2 Matrix{Union{Missing, Int64}}:
1 1
2 2
3 missing
julia> rstack(a, b, reverse(a), reverse(b); fill=NaN)
3×4 Matrix{Real}:
1 1 3 2
2 2 2 1
3 NaN 1 NaN

How to implement a decrementing for loop in Julia?

I know that in python I can do something as follows.
for i in range(10, 0, -1):
print(i)
Which will output:
10
9
8
7
6
5
4
3
2
1
I'm very much new to julia and I know I can create normal loops as follows.
for i=1:10
println(i)
end
Intuitively, I tried something like as follows (since I thought it behaved similar to python's range([start], stop[, step]) function).
for i=10:1:-1
println(i)
end
Although it didn't fail, it didn't print anything either. What am I doing wrong?
Is there an intuitive way to loop backwards in julia?
Try this:
julia> for i=10:-1:1
println(i)
end
10
9
8
7
6
5
4
3
2
1
or this
julia> for i=reverse(1:10)
println(i)
end
10
9
8
7
6
5
4
3
2
1
As #phipsgabler noted you can also use:
julia> range(10, 1, step=-1)
10:-1:1
to get the same result again (note though that you have to use 1 as a second index).
From my practice range is usually more useful with with length keyword argument:
julia> range(10, 1, length=10)
10.0:-1.0:1.0
(notice that in this case you get a vector of Float64 not Int)

Julia's Negative/Complement Indexing like R

Is there a functionality in Julia that's similar to R's negative indexing? In R, the code would be similar to:
x = 1:10
inds = c(1, 5, 7)
x[-inds]
[1] 2 3 4 6 8 9 10
I've found this to be extremely useful in numerous situations, especially for things such as sampling indices to create a testing/training set, but also for subindexing an array to exclude certain rows. So I am hoping there's something simple in Julia that can do the same.
This is similar to #Colin T Bower's answer and also only uses base Julia. Afraid it is not as elegant as your R example.
julia> minus(indx, x) = setdiff(1:length(x), indx)
minus (generic function with 1 method)
julia> x = collect(1:10)
10-element Array{Int64,1}:
1
2
3
4
5
6
7
8
9
10
julia> inds = [1, 5, 7]
3-element Array{Int64,1}:
1
5
7
julia> x[minus(inds, x)]
7-element Array{Int64,1}:
2
3
4
6
8
9
10
Not a feature of the base language, but see for example the package here: https://github.com/mbauman/InvertedIndices.jl

matlab find specific VALUES in an array

How to find out all array element indices equal to several values (>2)
For example, I have an array a=[1 2 3 4 5 5 4 3 2 2 2 1], I want to know the indices of all elements equal to b=[2 5]
Remember, I cannot use style like a==b(1) | a==b(2) because number of elements in b is arbitrary.
Do I have to use a for loop to do this?
You can use ismember (as Daniel said just seconds before I hit enter...) ;-)
a=[1 2 3 4 5 5 4 3 2 2 2 1];
b=[2 5];
c=find(ismember(a,b))
Output:
c =
2 5 6 9 10 11
If you want to do it more manually, you can use bsxfun:
c = find(any(bsxfun(#eq, a(:).', b(:)), 1));

Resources