Identify lines that have a repetition (unique function)

조회 수: 2 (최근 30일)
Alberto Acri
Alberto Acri 2022년 11월 21일
편집: Mathieu NOE 2022년 11월 21일
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

채택된 답변

Mathieu NOE
Mathieu NOE 2022년 11월 21일
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
Alberto Acri
Alberto Acri 2022년 11월 21일
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 2022년 11월 21일
편집: Mathieu NOE 2022년 11월 21일
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,:)

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

추가 답변 (1개)

Jan
Jan 2022년 11월 21일
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

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by