1 | %INTERSECT_OCTAVE Set intersection (octave only) |
---|
2 | |
---|
3 | %+ |
---|
4 | % |
---|
5 | % SEE ALSO |
---|
6 | % ======== |
---|
7 | % |
---|
8 | % tp_mlp_aerosols_startup.m_ |
---|
9 | % |
---|
10 | % .. _tp_mlp_aerosols_startup.m : tp_mlp_aerosols_startup.m.html |
---|
11 | % |
---|
12 | % intersect_octave.m_ |
---|
13 | % |
---|
14 | % .. _intersect_octave.m : intersect_octave.m.html |
---|
15 | % |
---|
16 | |
---|
17 | % TODO |
---|
18 | % ==== |
---|
19 | % |
---|
20 | % check for intersect.m evolutions in octave > 3.0.2 |
---|
21 | % |
---|
22 | % EVOLUTIONS |
---|
23 | % ========== |
---|
24 | % |
---|
25 | % $Id$ |
---|
26 | % |
---|
27 | % - fplod 2009-08-17T12:34:28Z zeus.locean-ipsl.upmc.fr (Linux) |
---|
28 | % |
---|
29 | % * creation from attachment of |
---|
30 | % http://www-old.cae.wisc.edu/pipermail/octave-maintainers/2008-September/008515.html |
---|
31 | % because octave 3.0.2 intersect.m does not accept rows argument |
---|
32 | % |
---|
33 | % remove redondant comments |
---|
34 | % |
---|
35 | % transformation to minimize mlint output |
---|
36 | % |
---|
37 | % |
---|
38 | %- |
---|
39 | |
---|
40 | function [c, ia, ib] = intersect_octave (a, b, r) |
---|
41 | |
---|
42 | % form a and b into sets |
---|
43 | |
---|
44 | if nargin == 3 % by rows |
---|
45 | |
---|
46 | if size(a,2) ~= size(b,2) |
---|
47 | error('The two matrices must have equal number of columns!'); |
---|
48 | end |
---|
49 | |
---|
50 | [a, ja] = unique (a,'rows'); |
---|
51 | [b, jb] = unique (b,'rows'); |
---|
52 | |
---|
53 | c = [ a ; b ]; |
---|
54 | [c, ic] = sortrows (c); % [a(:);b(:)](ic) == c |
---|
55 | |
---|
56 | if iscellstr (c) % how to handle this case |
---|
57 | ii = find ( strcmp (c(1:end-1), c(2:end) )); %% ??? |
---|
58 | else |
---|
59 | ii = find ( all( (c(1:end-1,:) == c(2:end,:)) , 2) ); |
---|
60 | end |
---|
61 | c = c(ii,:); % The answer |
---|
62 | else |
---|
63 | [a, ja] = unique (a); |
---|
64 | [b, jb] = unique (b); |
---|
65 | |
---|
66 | c = [a(:); b(:)]; |
---|
67 | [c, ic] = sort (c); % [a(:);b(:)](ic) == c |
---|
68 | |
---|
69 | if iscellstr (c) |
---|
70 | ii = find (strcmp (c(1:end-1), c(2:end))); |
---|
71 | else |
---|
72 | ii = find (c(1:end-1) == c(2:end)); |
---|
73 | end |
---|
74 | c = c(ii); % The answer |
---|
75 | |
---|
76 | if size (b, 1) == 1 || size (a, 1) == 1 |
---|
77 | c = c.'; |
---|
78 | end |
---|
79 | end |
---|
80 | |
---|
81 | ia = ja(ic(ii)); % a(ia) == c |
---|
82 | ib = jb(ic(ii+1) - length (a)); % b(ib) == c |
---|
83 | |
---|
84 | endfunction |
---|