code that would identify the identical rows

조회 수: 1 (최근 30일)
Alberto Acri
Alberto Acri 2022년 11월 18일
댓글: Alberto Acri 2022년 11월 18일
Hello! I have a text file (.txt) consisting of 'M' rows and 3 columns. I would like to write a code that allows to identify the identical rows (the highlighted rows) and keep only one copy, of each repeated row, within the text file.
As a result, if a row repeats one or more times, I have to recreate the text file (.txt):
- keeping the lines that do not repeat
- bringing back only once the lines that are repeated
For example:
I thank you if anyone can help me!

채택된 답변

Arif Hoq
Arif Hoq 2022년 11월 18일
a=table2array(readtable("file.txt"));
output=unique(a,'rows','stable')
output = 12×3
233.0000 353.0000 1.4912 233.0000 352.0000 1.4912 353.0000 296.0000 64.1228 353.0000 295.0000 64.1228 353.0000 216.0000 64.1228 121.0000 217.0000 162.5439 121.0000 216.0000 162.5439 121.0000 215.0000 162.5439 119.0000 274.0000 184.9123 119.0000 273.0000 184.9123
  댓글 수: 3
Arif Hoq
Arif Hoq 2022년 11월 18일
a=table2array(readtable("file.txt"));
[output,ia,ic]=unique(a,'rows','stable')
output = 12×3
233.0000 353.0000 1.4912 233.0000 352.0000 1.4912 353.0000 296.0000 64.1228 353.0000 295.0000 64.1228 353.0000 216.0000 64.1228 121.0000 217.0000 162.5439 121.0000 216.0000 162.5439 121.0000 215.0000 162.5439 119.0000 274.0000 184.9123 119.0000 273.0000 184.9123
ia = 12×1
1 2 3 4 5 6 8 9 11 12
ic = 15×1
1 2 3 4 5 6 1 7 8 4
index vector ic represents the repetition. Here indexes 1,7,13 are the duplicate rows as well 4,10.
Alberto Acri
Alberto Acri 2022년 11월 18일
Thank you @Arif Hoq!
Is it possible to directly identify the lines that have repetition?
In the case of 'file.txt' have something like this:
coordinates repeatability
2.3300000e+02 3.5300000e+02 1.4912281e+00 2
3.5300000e+02 2.9500000e+02 6.4122807e+01 1

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by