1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) INRIA
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution. The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
function [x_out,ka_out,kb_out]=intersect(a_in,b_in,orient)
// returns the vector of common values of two vectors
// Author : Serge Steer INRIA
if ( (a_in == [])| (b_in == []) ) then
x_out=[];
ka_out=[];
kb_out=[];
return
end
if argn(2)<3 then
//remove duplicate values in a_in and b_in
[a,ka]=unique(matrix(a_in,1,-1));
[b,kb]=unique(matrix(b_in,1,-1));
kab=[ka, -kb];
//find duplicated values in [a_in,b_in],
[x,ksort] = gsort([a,b],'g','i'); //sort the array
kab = kab(ksort);//apply [a_in,b_in] sorting permutation to kab
keq = find( x(2:$) == x(1:$-1) ); // find consecutive equal values index
if keq == [] then
//the intersection is empty
x_out = [];
ka_out = [];
kb_out = [];
else
x_out =x(keq); //the intersection values in increasing order
if argn(1)>1 then //build the output index
// each duplicated value appear twice and only twice and in
// consecutive positions keq(i) and keq(i)+1 in the sorted array x
kab=kab([keq keq+1]);
//the positive values correspond to a_in index while the negative to b_in index.
ka_out = kab(kab>0); //select index of intersection elements in a_in
kb_out = -kab(kab<0); //select index of intersection elements in b_in
//insure that a_in(ka_out)==x_out and b_in(kb_out)==x_out.
//I was'nt able to find a simple way.
[s,k]=gsort(a_in(ka_out),'g','i'); ka_out=ka_out(k)
[s,k]=gsort(b_in(kb_out),'g','i'); kb_out=kb_out(k)
end
end
elseif orient==1|orient=="r" then
//remove duplicate rows in a_in and b_in
[a,ka]=unique(a_in,"r");
[b,kb]=unique(b_in,"r");
kab=[ka; -kb];
//find duplicated rows in [a_in;b_in],
[x,ksort] = gsort([a;b],'lr','i'); //sort the rows
kab = kab(ksort);//apply [a_in,b_in] sorting permutation to kab
keq = find(and(x(2:$,:) == x(1:$-1,:),'c')) // find index of consecutive equal values
if keq == [] then
//the intersection is empty
x_out = [];
ka_out = [];
kb_out = [];
else
x_out =x(keq,:); //the intersection values in increasing order
if argn(1)>1 then //build the output index
// each duplicated value appear twice and only twice and in
// consecutive positions keq(i) and keq(i)+1 in the sorted array x
kab=kab([keq keq+1]);
//the positive values correspond to a_in index while the negative to b_in index.
ka_out = kab(kab>0); //select index of intersection elements in a_in
kb_out = -kab(kab<0); //select index of intersection elements in b_in
//insure that a_in(ka_out,:)==x_out and b_in(kb_out,:)==x_out.
//I was'nt able to find a simple way.
[s,k]=gsort(a_in(ka_out,:),'lr','i'); ka_out=ka_out(k)
[s,k]=gsort(b_in(kb_out,:),'lr','i'); kb_out=kb_out(k)
end
end
elseif orient==2|orient=="c" then
//remove duplicate columns in a_in and b_in
[a,ka]=unique(a_in,"c");
[b,kb]=unique(b_in,"c");
kab=[ka, -kb];
//find duplicated rows in [a_in;b_in],
[x,ksort] = gsort([a b],'lc','i'); //sort the rows
kab = kab(ksort);//apply [a_in,b_in] sorting permutation to kab
keq = find(and(x(:,2:$) == x(:,1:$-1),'r')) // find index of consecutive equal values
if keq == [] then
//the intersection is empty
x_out = [];
ka_out = [];
kb_out = [];
else
x_out =x(:,keq); //the intersection values in increasing order
if argn(1)>1 then //build the output index
// each duplicated value appear twice and only twice and in
// consecutive positions keq(i) and keq(i)+1 in the sorted array x
kab=kab([keq keq+1]);
//the positive values correspond to a_in index while the negative to b_in index.
ka_out = kab(kab>0); //select index of intersection elements in a_in
kb_out = -kab(kab<0); //select index of intersection elements in b_in
//insure that a_in(ka_out,:)==x_out and b_in(kb_out,:)==x_out.
//I was'nt able to find a simple way.
[s,k]=gsort(a_in(:,ka_out),'lc','i'); ka_out=ka_out(k)
[s,k]=gsort(b_in(:,kb_out),'lc','i'); kb_out=kb_out(k)
end
end
else
error(msprintf(gettext("%s: Wrong value for input argument #%d: %d,''%s'',%d or ''%s'' expected\n"),'intersect',3,1,"r",2,"c"));
end
endfunction
|