559
|
1 function y = intersection(a,b) |
|
2 |
|
3 # usage: intersection(a,b) |
|
4 # |
|
5 # Returns the intersection of sets a and b. |
|
6 # |
|
7 # See - create_set, union, complement |
|
8 |
|
9 if (nargin != 2) |
|
10 error("usage: intersection(a,b)"); |
|
11 endif |
|
12 |
|
13 if(isempty(a) || isempty(b)) |
|
14 y = []; |
|
15 return; |
|
16 endif |
|
17 |
|
18 a = create_set(a); |
|
19 b = create_set(b); |
|
20 |
|
21 if(length(a) < length(b)) |
|
22 yindex = 1; |
|
23 y = zeros(1,length(a)); |
|
24 for index = 1:length(a) |
|
25 if(any(b == a(index))) |
|
26 y(yindex++) = a(index); |
|
27 endif |
|
28 endfor |
|
29 else |
|
30 yindex = 1; |
|
31 y = zeros(1,length(b)); |
|
32 for index = 1:length(b) |
|
33 if(any(a == b(index))) |
|
34 y(yindex++) = b(index); |
|
35 endif |
|
36 endfor |
|
37 endif |
|
38 |
|
39 y = y(1:(yindex-1)); |
|
40 |
|
41 endfunction |