<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Hi,<br>
<br>
----- Message d'origine ----- <br>
De : Sébastien Bihorel <br>
Date : 08/11/2009 12:29:
<blockquote
 cite="mid:ecd262690911080329xbd1fabcv367c8a2b0eada979@mail.gmail.com"
 type="cite">Dear Scilab users,<br>
  <br>
Is there an easy method to emulate the Matlab
ismember function to match numerical vectors to one another? I've 
tried several things with intersect but it does not seem to give
consistent results.<br>
  <br>
Here is a section of the Matlab help about ismember:<br>
[tf, loc] = ismember(A, S, ...) returns an array loc containing the <b>highest
  </b>index in S for each element in A that is a member of S.<br>
  <br>
  <br>
Here is an example of Scilab code that I tried<br>
a = [5 3 1 7 2 9 2 6 4 1 3 6 8 0 1 4];<br>
b=1:5;<br>
[v,ka,kb]=intersect(a,b,'c');<br>
  <br>
Surprisingly, ka is a mix of low, middle or high indexes... There must
a logic behind that, but I do not see the pattern.<br>
  <br>
  <br>
I am looking to extract only the highest indexes. Any help would be
appreciated. <br>
  <br>
Sebastien
</blockquote>
Here is a function that works like Matlab for giving loc :<br>
-----------<br>
function loc = ismember(A,S, highest)<br>
   // loc = ismember(A, S, highest)<br>
   //<br>
   // A : Matrix of booleans, integers, reals, complexes, polynomes<br>
   // S : Matrix of same datatype than S<br>
   // highest : Scalar boolean<br>
   //<br>
   // ismember() returns a matrix loc of A's format. loc(i,j) = linear <br>
   //  index in S of the first (highest ==%F) | last (highest==%T) <br>
   //  occurrence of A(i,j). Zero is returned in A(i,j) if no
occurrence is found<br>
   //<br>
   LA=length(A);<br>
   LS=length(S);<br>
   A2=matrix(A,LA,1)*ones(1,LS) ;<br>
   S2=(matrix(S,LS,1)*ones(1,LA)).' ;<br>
   d01  =double(A2==S2);<br>
   S_ind=ones(LA,1)*(1:LS);<br>
   tmp=d01.*S_ind;<br>
   if highest, <br>
       tmp2=max(tmp,'c'); <br>
   else <br>
       tmp(tmp==0)=%inf;    // removes zeros as min values<br>
       tmp2=min(tmp,'c');<br>
       tmp2(tmp2==%inf)=0;<br>
   end<br>
   loc=matrix(tmp2,size(A));<br>
endfunction<br>
--------<br>
ismember() works with any datatype, as long as elements <br>
can be multiplied by reals and compared each other with "==".<br>
<br>
A test with reals gives:<br>
-->A,S<br>
 A  =<br>
 <br>
    7.    3.  <br>
    2.    0.  <br>
    8.    1.  <br>
 S  =<br>
 <br>
    5.    8.    0.    4.  <br>
    3.    4.    7.    7.  <br>
    3.    6.    6.    2.  <br>
    7.    5.    5.    8.  <br>
 <br>
-->ismember(A,S,%f)<br>
 ans  =<br>
 <br>
    4.     2.  <br>
    15.    9.  <br>
    5.     0.  <br>
 <br>
-->ismember(A,S,%t)<br>
 ans  =<br>
 <br>
    14.    3.  <br>
    15.    9.  <br>
    16.    0.  <br>
<br>
HTH,<br>
<br>
Regards<br>
Samuel<br>
<br>
</body>
</html>