Identify lines that have a repetition (unique function)

7 views (last 30 days)
From the attached file, I applied this code to eliminate repeated rows:
a=table2array(readtable("file.txt"));
[output,ia,ic]=unique(a,'rows','stable')
repetition = ic;
Is it possible to directly identify the lines that have repetition?
For '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

Accepted Answer

Mathieu NOE
Mathieu NOE on 21 Nov 2022
hello
simply this
now the 4th column is your repetition quantity
a=readmatrix("file.txt");
[output,ia,ic]=unique(a,'rows','stable')
B = unique(ic);
Ncount = histc(ic, B); % this willgive the number of occurences of each unique element
out = [output Ncount]
out =
233.0000 353.0000 1.4912 3.0000
233.0000 352.0000 1.4912 1.0000
353.0000 296.0000 64.1228 1.0000
353.0000 295.0000 64.1228 2.0000
353.0000 216.0000 64.1228 1.0000
121.0000 217.0000 162.5439 1.0000
121.0000 216.0000 162.5439 1.0000
121.0000 215.0000 162.5439 1.0000
119.0000 274.0000 184.9123 1.0000
119.0000 273.0000 184.9123 1.0000
369.0000 176.0000 246.0526 1.0000
371.0000 184.0000 246.0526 1.0000
  2 Comments
Alberto Acri
Alberto Acri on 21 Nov 2022
I thank you @Mathieu NOE. That's exactly what I was asking.
Could you also tell me how I can display only those rows that have in the fourth column values greater than or equal to 2 listed in 'out'?
For example, from 'out' I would like to save in 'out2' only these:
233 353 1.4912 3
353 295 64.122 2
Thanks!!!
Mathieu NOE
Mathieu NOE on 21 Nov 2022
Edited: Mathieu NOE on 21 Nov 2022
simply use logical array to select the appropriate lines
a=readmatrix("file.txt");
[output,ia,ic]=unique(a,'rows','stable')
B = unique(ic);
Ncount = histc(ic, B); % this willgive the number of occurences of each unique element
out = [output Ncount] % the 4th column is your repetition quantity
idx = Ncount>=2; % logical array : select only lines for which repeat >= 2
out2 = out(idx,:)

Sign in to comment.

More Answers (1)

Jan
Jan on 21 Nov 2022
data = [ 2.3300000e+02 3.5300000e+02 1.4912281e+00
2.3300000e+02 3.5200000e+02 1.4912281e+00
3.5300000e+02 2.9600000e+02 6.4122807e+01
3.5300000e+02 2.9500000e+02 6.4122807e+01
3.5300000e+02 2.1600000e+02 6.4122807e+01
1.2100000e+02 2.1700000e+02 1.6254386e+02
2.3300000e+02 3.5300000e+02 1.4912281e+00
1.2100000e+02 2.1600000e+02 1.6254386e+02
1.2100000e+02 2.1500000e+02 1.6254386e+02
3.5300000e+02 2.9500000e+02 6.4122807e+01
1.1900000e+02 2.7400000e+02 1.8491228e+02
1.1900000e+02 2.7300000e+02 1.8491228e+02
2.3300000e+02 3.5300000e+02 1.4912281e+00
3.6900000e+02 1.7600000e+02 2.4605263e+02
3.7100000e+02 1.8400000e+02 2.4605263e+02];
repeated = isMultipleRow(data)
repeated = 15×1 logical array
1 0 0 1 0 0 1 0 0 1 0 0 1 0 0
function T = isMultipleRow(A)
nA = height(A);
T = false(nA, 1);
[S, idx] = sortrows(A);
m = [false; ~any(diff(S, 1, 1), 2)];
if any(m) % Any repeated elements found:
ini = strfind(m.', [false, true]);
m(ini) = true; % Mark 1st occurence in addition
T(idx) = m; % Restore original order
end
end

Categories

Find more on Graphics Objects in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!