Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get matrix from textfile in Mathematica

What's the easiest way to turn this text file into matrix? It has one row per line, where O means 0 and X means 1

like image 501
Yaroslav Bulatov Avatar asked Feb 24 '11 22:02

Yaroslav Bulatov


2 Answers

$url = "http://hyperpublic.com/challenge2input.txt";
StringCases[Import[$url, "Lines"], {"O" -> 0, "X" -> 1}]
like image 125
WReach Avatar answered Sep 28 '22 09:09

WReach


I first saved that text in a file tmp.txt.

In[180]:= words = ReadList["~danl/tmp.txt", Word];
vals = Map[Characters, words] /. {"O" -> 0, "X" -> 1};

In[182]:= vals[[1]]
Out[182]= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

Daniel Lichtblau Wolfram Research

like image 26
Daniel Lichtblau Avatar answered Sep 28 '22 07:09

Daniel Lichtblau