2011-02-24 74 views
6

this文本文件轉換爲矩陣的最簡單方法是什麼?它具有每行一列,其中O意味着0X意味着1在Mathematica中從文本文件中獲取矩陣

+0

你使用的是什麼語言? – 2011-02-24 22:20:17

+2

Mathematica語言 – 2011-02-24 22:22:53

+1

你應該對待自己的數字鍵盤:) – 2011-02-24 23:21:17

回答

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} 

丹尼爾Lichtblau 沃爾夫勒姆研究

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

等待,你可以使用URL作爲參數'Import'?多麼酷啊? – Pillsy 2011-02-25 14:10:41

+0

祝賀你的10k!我不得不找到一個答案,我還沒有upvoted讓你感動:) – abcd 2011-11-27 18:46:04

+0

@Yoda謝謝! :) – WReach 2011-11-27 19:15:01