I have text file which includes a matrix. I want to read it in julia as a matrix.
The text file is like:
0 0 0 0 0 0 0
1 0 0 0 0 0 0
1 0 0 0 0 0 1
1 0 0 0 1 1 0
In matlab you can do the following to create matrix M
:
file='name.txt';
[M] = load(file);
How to do same thing in Julia?
To read in data from a text file, you can use readdlm() . There are also a number of Julia packages specifically designed for reading and writing data to files, including DataFrames. jl and CSV.
Julia provides a very simple notation to create matrices. A matrix can be created using the following notation: A = [1 2 3; 4 5 6]. Spaces separate entries in a row and semicolons separate rows. We can also get the size of a matrix using size(A).
Using open() function In order to store the data in a text file, we need to use open() function with “w” flag which allows us to write output to a file.
shell> cat /tmp/m.txt
0 0 0 0 0 0 0
1 0 0 0 0 0 0
1 0 0 0 0 0 1
1 0 0 0 1 1 0
julia> m = readdlm("/tmp/m.txt")
4x7 Array{Float64,2}:
0.0 0.0 0.0 0.0 0.0 0.0 0.0
1.0 0.0 0.0 0.0 0.0 0.0 0.0
1.0 0.0 0.0 0.0 0.0 0.0 1.0
1.0 0.0 0.0 0.0 1.0 1.0 0.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With