1 | function d = duplicated(x)
|
---|
2 | %DUPLICATED Find duplicated rows.
|
---|
3 | % DUPLICATED(x) returns a vector d such that d(i) = 1 if x(i,:) is a
|
---|
4 | % duplicate of an earlier row.
|
---|
5 | %
|
---|
6 | % Examples:
|
---|
7 | % duplicated([2 7 8 7 1 2 8]') = [0 0 0 1 0 1 1]'
|
---|
8 | % duplicated([0 0 1 1 0; 0 1 0 1 1]') = [0 0 0 0 1]'
|
---|
9 | % duplicated(eye(100))
|
---|
10 | % duplicated(kron((1:3)',ones(3)))
|
---|
11 | %
|
---|
12 | % You can simulate unique(x) or unique(x,'rows') by x(~duplicated(x)).
|
---|
13 | % The difference is that the latter form will not sort the contents of x,
|
---|
14 | % as unique will.
|
---|
15 | %
|
---|
16 | % See also UNIQUE.
|
---|
17 |
|
---|
18 | % (c) Microsoft Corporation. All rights reserved.
|
---|
19 |
|
---|
20 | % This function is not well optimized.
|
---|
21 | % In particular, it is slower than unique.
|
---|
22 |
|
---|
23 | [nr,nc] = size(x);
|
---|
24 | if nc == 1
|
---|
25 | d = duplicated1(x);
|
---|
26 | return;
|
---|
27 | end
|
---|
28 | if nr == 1
|
---|
29 | d = 0;
|
---|
30 | return;
|
---|
31 | end
|
---|
32 | hash = x*rand(nc,1);
|
---|
33 | [dummy,ord] = sort(hash);
|
---|
34 | xo = x(ord,:);
|
---|
35 | %d = [0 all(diff(xo)==0,2)'];
|
---|
36 | d = [0 all(xo(1:end-1,:)==xo(2:end,:),2)'];
|
---|
37 | dd = diff([d 0]);
|
---|
38 | dstart = find(dd > 0);
|
---|
39 | dend = find(dd < 0);
|
---|
40 | % loop each run of duplicated columns
|
---|
41 | for i = 1:length(dstart)
|
---|
42 | % place the zero at the first element in the original order
|
---|
43 | d(dstart(i)) = 1;
|
---|
44 | d(dstart(i)-1 + argmin(ord(dstart(i):dend(i)))) = 0;
|
---|
45 | end
|
---|
46 | d(ord) = d;
|
---|
47 | d = d';
|
---|
48 | %d = duplicated1(hash);
|
---|
49 |
|
---|
50 | function d = duplicated1(x)
|
---|
51 | % special case where x is a column vector.
|
---|
52 |
|
---|
53 | [s,ord] = sort(x);
|
---|
54 | d = zeros(size(x));
|
---|
55 | d(ord) = [0; s(1:end-1)==s(2:end)];
|
---|
56 | %d(ord) = [0; diff(s)==0];
|
---|