removing duplicate data

[EDIT: 20110513 00:34 CDT - reformat - WDR]
I have a text file containing some data and there are some duplicates
line1 : 123 456 789
line2 : 123 456 789
line3 : 234 567 890
line4 : 123 456 789
line5 : 456 789 012
how can I remove the repeated data and save back to my txt file?

回答 (1 件)

Matt Fig
Matt Fig 2011 年 3 月 25 日

0 投票

Load the data, call the UNIQUE function with the rows option, then save the result.

9 件のコメント

Hoa
Hoa 2011 年 3 月 25 日
I know unique can be used, but I need to compare the 3 different values in every row, that means line1 123 & line2 123, line1 456 & line2 456
and line1 789 & line2 789.
Matt Fig
Matt Fig 2011 年 3 月 25 日
Then you need to be more specific. Given the data:
123 456 789
123 456 789
234 567 890
123 456 789
456 789 012
What do you expect the output to be? Do you want the output to be:
123
456
789
234
567
890
012
or, did you want the output to be:
123 456 789
234 567 890
456 789 012
Hoa
Hoa 2011 年 3 月 25 日
yes i mean the second output
Jan
Jan 2011 年 3 月 25 日
Then UNIQUE(Data, 'rows') should work.
Matt Fig
Matt Fig 2011 年 3 月 25 日
fid = fopen('mydata.txt','r');
T = textscan(fid,'%f%f%f','collectoutput',1);T = T{1};
U = unique(T);
fclose(fid);
fid = fopen('mydata.txt','w');
fprintf(fid,'%g\r',U.');
fclose(fid);
Hoa
Hoa 2011 年 3 月 26 日
output generated with this is:
line1 1
line2 2
line3 3
line4 4
it output a single digit instead of the whole junk of values
Walter Roberson
Walter Roberson 2011 年 3 月 26 日
Please show your current code.
Hoa
Hoa 2011 年 3 月 27 日
I used the code by matt fig
Walter Roberson
Walter Roberson 2011 年 3 月 27 日
Change the line
fprintf(fid,'%g\r',U.');
to
fprintf(fid,'%g\n',U.');

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

質問済み:

Hoa
2011 年 3 月 25 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by