史蒂夫(Steve)与MATLAB进行图像处理

图像处理概念,算法和MATLAB

国际象棋和一些文本文件操纵

Here's an image of a chess position:

这与今天的博客文章将接近图像处理。因为这篇文章实际上是关于文本处理的。

It seems like a lot of computational tasks in engineering and science involve manipulating data in text files. This weekend I had such a task, although I must admit that it had nothing to do with engineering or science. Even so, I thought the task would be a good illustration of some basic text processing techniques.

我有一个文本文件tactics.pgn,它是国际象棋策略难题的数据库。这是文件中的2,759至2,784行。

[event“?”] [site“?”] [date' ??????????] [round'?] [eventDate“ 2006.05.20”] [FEN“ 2B1R3/ppk4p/8/2q2p2/2br2n1/2qp2n1/p4ppp/p4ppp/6k1 W--0 1”
1。Rxc8+ Kxc8 2.Be6+ Kd8 3.Qxc5 *
[event“?”] [site“?”] [date' ??????????] [round'?] [FEN“ 3RR1K1/P1P2PPP/Q2B1Q2/8/3NP3/4P3/P3/PP3PPP/R1B2RK1 B- 0 1”]
1。。。Bxh2+ 2.Kxh2 Qxa6 *

这些线存储两个下棋的位置。第一个我s the position I showed above, with White to move. Can you figure out how White wins by capturing the Bishop on c8 with his rook? (If you're interested in how the position is encoded in the text, see theWikipedia article on Forsyth-Edwards Notation,或fen。)

I wanted to shuffle the positions in this file randomly. None of the chess software programs that I have can do this, so I decided to tackle it with MATLAB. (Fun thing to do on a Saturday morning, right?)

OK, so here's the basic procedure:

  1. 将整个文件读为MATLAB。
  2. 将数据分成块,每个位置一个部分。
  3. 随机重新排列块。
  4. Write out the rearranged chunks to a new text file.

There are a lot of different ways to do this. Here's what I came up with.

首先,在整个文件中阅读。功能fileReadis just the ticket.

字符= fileRead('tactics.pgn');大小(字符)
ANS = 1 106394

You can see that there are about 106,000 characters in the file. Let's split the data into lines usingstrsplit

lines = strsplit(characters,'\n')'; size(lines)
ANS = 4838 1

There are about 4,800 lines of text. But how many positions are there? I'm going to find the starting line of each position by searching for the string "[Event " at the beginning a line. It's time forregexp

IDX= regexp(lines,'^[Event ');IDX(1:15)
ans = [1] [] [] [] [] [] [] [] [] [] [] [] [] [1] [] [] [] [] []

这表明我们在文件的前15行中两次找到该字符串。IDXis a cell array, so I'll usecellfunandfindto identify all the lines that contain the matching string. Each of these lines is the start of an entry for one chess position.

first_lines = find(〜cellfun(@isementy,idx));first_lines(1:3)
ANS = 1 12 23

因此,从第1、12和23行开始有位置。

Next, I'll make a cell array such that that each cell contains all the lines for one position. To make the for-loop work, I'm going to add an "extra" value to the first_lines vector that points to a nonexistent line just past the of the file.

first_lines(end+1) = length(lines) + 1;为了k = 1:长度(first_lines)-1位置{k} = lines(first_lines(k):first_lines(k+1)-1);结尾

让我们看一下我们现在拥有的东西。

size(positions)
ANS = 1 421

文件中有421个位置。例如:

位置{205}
ans = '[Event "?"]' '[Site "?"]' '[Date "????.??.??"]' '[Round "?"]' '[White "Diagram 211"]' '[Black "Bain"]' '[Result "*"]' '[EventDate "2006.05.20"]' '[FEN "2b1R3/ppk4p/8/2q2p2/2Br2n1/2QP2N1/P4PPP/6K1 w - - 0 1"]' '[SetUp "1"]' '[SourceDate "2011.02.22"]' '1.Rxc8+ Kxc8 2.Be6+ Kd8 3.Qxc5 *'

同样,这是本文顶部显示的位置。

Getting near the end, now. It's time to rearrange the positions. Before I do that, though, I'll shuffle the random number generator. I only do this so that if I repeat these steps in a new MATLAB session, I'll be sure to get a different result. After shuffling the random number generator usingrng,快速打电话给兰德珀随机重新安排位置。

rngshuffleshuffled_positions =位置(randperm(长度(位置)));

我们到达了最后一步:将改组的位置写成一个新文件。

fid = fopen('shuffled_tactics.pgn',,,,'W');为了k = 1:长度(shuffled_positions)位置= shuffled_positions {k};为了p = 1:length(position) fprintf(fid,'%s\n',,,,position{p});结尾结尾fclose(fid);

And that's it!

读取文本,以某种有用的方式操纵它,然后将结果写回去 - 使用多个基本MATLAB函数完成的常见计算任务。




与Matlab®R2013B一起出版

|
  • 打印
  • 发送电子邮件

Comments

要发表评论,请单击here登录您的数学帐户或创建一个新帐户。