在Matlab中比较两列包含数字的文本文件

时间:2014-08-14 11:16:04

标签: matlab text numbers compare

我有2个文本文件(a.txt,b.txt),其中包含一些数字列和一个标题行(每列有一个标题,如下所示)。我想要匹配第二列。在带有第一列的a.txt中。在b.txt中获取b.txt中所有匹配的行。 col.-gr中的数值不在a.txt或b.txt中重复。

a.txt
—————
gc   gr
1    5
3    8
3    4
3    9


b.txt
—————
gr   c1   c2
1    12   32
3    21   23
7    33   12
8    54   45
9    99   65
34   43   76
56   80   24
5    32   80
32   15   23
4    11   31

我希望b.txt中匹配的行与 -

完全相同
5    32   80
8    54   45
4    11   31
9    99   65

1 个答案:

答案 0 :(得分:0)

试试这个

id = fopen('a.txt','r'); 
A = cell2mat(textscan(id,'%d %d','headerlines',1));
fclose(id);
id = fopen('b.txt','r'); 
B = cell2mat(textscan(id,'%d %d %d','headerlines',1));
fclose(id);

out_ = cell2mat(arrayfun(@(i)(B(find(A(i,2) == B(:,1),1,'first'),:)),1:size(A,1),'uni',0)');
相关问题