changeset 24617:89b0a53258b7

ismember: use both real and imaginary parts instead of magnitude (bug #52437)
author Carnë Draug <carandraug@octave.org>
date Thu, 18 Jan 2018 13:13:53 +0000
parents 66729bff9877
children c241918d4c92
files scripts/set/ismember.m
diffstat 1 files changed, 45 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/set/ismember.m	Wed Jan 17 21:12:47 2018 +0100
+++ b/scripts/set/ismember.m	Thu Jan 18 13:13:53 2018 +0000
@@ -80,6 +80,21 @@
     print_usage ();
   endif
 
+  ## lookup() uses absolute values for complex input so we handle the
+  ## real and imaginary parts separately (bug #52437)
+  if (iscomplex (a) || iscomplex (s))
+    real_argout = cell (nargout, 1);
+    imag_argout = cell (nargout, 1);
+    [real_argout{:}] = ismember (real (a), real (s), varargin{:});
+    [imag_argout{:}] = ismember (imag (a), imag (s), varargin{:});
+    tf = real_argout{1} & imag_argout{1};
+    if (isargout (2))
+      s_idx = zeros (size (real_argout{2}));
+      s_idx(tf) = real_argout{2}(tf);
+    endif
+    return
+  endif
+
   ## lookup() does not handle logical values
   if (islogical (a))
     a = uint8 (a);
@@ -237,3 +252,33 @@
 %!test <*51187>
 %! abc = ['a '; 'b '; 'c '];
 %! assert (ismember (abc, {abc}), [false; false; false]);
+
+%!test <*52437>
+%! [tf, s_idx] = ismember ([5 4-3j 3+4j], [5 4-3j 3+4j]);
+%! assert (tf, logical ([1 1 1]))
+%! assert (s_idx, [1 2 3])
+%!
+%! [tf, s_idx] = ismember ([5 4-3j 3+4j], 5);
+%! assert (tf, logical ([1 0 0]))
+%! assert (s_idx, [1 0 0])
+%!
+%! [tf, s_idx] = ismember ([5 5 5], 4-3j);
+%! assert (tf, logical ([0 0 0]))
+%! assert (s_idx, [0 0 0])
+%!
+%! [tf, s_idx] = ismember ([5 4-3j 3+4j; 5i 6 6i], [5 6]);
+%! assert (tf, logical ([1 0 0; 0 1 0]))
+%! assert (s_idx, [1 0 0; 0 2 0])
+%!
+%! [tf, s_idx] = ismember ([5 4-3j 3+4j; 5 4-3j 3+4j], [5 5 5], "rows");
+%! assert (tf, logical ([0; 0]))
+%! assert (s_idx, [0; 0])
+%!
+%! [tf, s_idx] = ismember ([5 5 5], [5 4-3j 3+4j; 5 5 5], "rows");
+%! assert (tf, true)
+%! assert (s_idx, 2)
+%!
+%! [tf] = ismember ([5 4-3j 3+4j], 5);
+%! assert (tf, logical ([1 0 0]))
+%! [~, s_idx] = ismember ([5 4-3j 3+4j], 5);
+%! assert (s_idx, [1 0 0])