Clear Filters
Clear Filters

code that would identify the identical rows

1 view (last 30 days)
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!

Accepted Answer

Arif Hoq
Arif Hoq on 18 Nov 2022
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 Comments
Arif Hoq
Arif Hoq on 18 Nov 2022
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 on 18 Nov 2022
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

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!