Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading data matrix from text file in Julia

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?

like image 426
hmi2015 Avatar asked Feb 10 '16 07:02

hmi2015


People also ask

How do I read a text file 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.

How do you enter a Matrix into Julia?

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).

How do I write to a text file in Julia?

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.


1 Answers

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
like image 144
daycaster Avatar answered Sep 22 '22 20:09

daycaster