从Mathematica中的文本文件中获取矩阵

时间:2011-02-24 22:18:54

标签: wolfram-mathematica

this文本文件转换为矩阵的最简单方法是什么?每行有一行,O表示0X表示1

2 个答案:

答案 0 :(得分:10)

$url = "http://hyperpublic.com/challenge2input.txt";
StringCases[Import[$url, "Lines"], {"O" -> 0, "X" -> 1}]

答案 1 :(得分:9)

我首先将该文本保存在文件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