source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/lightspeed/duplicated.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 1.4 KB
Line 
1function 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);
24if nc == 1
25  d = duplicated1(x);
26  return;
27end
28if nr == 1
29  d = 0;
30  return;
31end
32hash = x*rand(nc,1);
33[dummy,ord] = sort(hash);
34xo = x(ord,:);
35%d = [0 all(diff(xo)==0,2)'];
36d = [0 all(xo(1:end-1,:)==xo(2:end,:),2)'];
37dd = diff([d 0]);
38dstart = find(dd > 0);
39dend = find(dd < 0);
40% loop each run of duplicated columns
41for 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;
45end
46d(ord) = d;
47d = d';
48%d = duplicated1(hash);
49
50function d = duplicated1(x)
51% special case where x is a column vector.
52
53[s,ord] = sort(x);
54d = zeros(size(x));
55d(ord) = [0; s(1:end-1)==s(2:end)];
56%d(ord) = [0; diff(s)==0];
Note: See TracBrowser for help on using the repository browser.