changeset 9364:6c255e51ef7e

improve & fix ismember
author Jaroslav Hajek <highegg@gmail.com>
date Fri, 19 Jun 2009 07:46:37 +0200
parents da465c405d84
children 3cf6a0f9a7a7
files scripts/ChangeLog scripts/set/ismember.m
diffstat 2 files changed, 22 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Fri Jun 19 07:29:19 2009 +0200
+++ b/scripts/ChangeLog	Fri Jun 19 07:46:37 2009 +0200
@@ -1,3 +1,7 @@
+2009-06-19  Jaroslav Hajek  <highegg@gmail.com>
+
+	* set/ismember.m: Properly handle NaNs. Fix test.
+
 2009-06-18  Ben Abbott <bpabbott@mac.com>
 
 	* plot/__go_draw_axes__.m: Change the default x11 fontspec from '*,0'
--- a/scripts/set/ismember.m	Fri Jun 19 07:29:19 2009 +0200
+++ b/scripts/set/ismember.m	Fri Jun 19 07:46:37 2009 +0200
@@ -97,15 +97,32 @@
     else
       [s, is] = sort (s);
     endif
+
+    ## sort out NaNs
+    if (isreal (s) && ! isempty (s) && isnan (s(end)))
+        s = s(1:end - sum (isnan (s)));
+    endif
     
+    if (isreal (a))
+      anan = isnan (a);
+      a(anan) = 0;
+    endif
+
     if (nargout > 1)
       a_idx = lookup (s, a, "m");
       tf = logical (a_idx);
       if (! isempty (is))
         a_idx(tf) = is (a_idx(tf));
       endif
+      if (isreal (a))
+        tf(anan) = false;
+        a_idx(anan) = 0;
+      endif
     else
       tf = lookup (s, a, "b");
+      if (isreal (a))
+        tf(anan) = false;
+      endif
     endif
 
   elseif (nargin == 3 && strcmpi (rows_opt, "rows"))
@@ -200,7 +217,7 @@
 
 %!test
 %! [result, a_idx] = ismember ("1.1", "0123456789.1");
-%! assert (all (result == logical ([1, 1, 1])) && all (a_idx == [2, 11, 2]));
+%! assert (all (result == logical ([1, 1, 1])) && all (a_idx == [12, 11, 12]));
 
 %!test
 %! [result, a_idx] = ismember([1:3; 5:7; 4:6], [0:2; 1:3; 2:4; 3:5; 4:6], 'rows');