bugGNU Octave - Bugs: bug #53924, ismember error with mixed numeric...

 
 

bug #53924: ismember error with mixed numeric and char arrays inputs

Submitter:  Guillaume <gyom>
Submitted:  Wed 16 May 2018 11:03:42 AM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  Guillaume Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Fri 14 Dec 2018 05:03:45 PM UTC, comment #4: 

I checked in the patch from comment #1 under your name here https://hg.savannah.gnu.org/hgweb/octave/rev/00924e6e513e.

Marking as fixed and closing report.

Rik <rik5>
Group administrator
Fri 14 Dec 2018 09:50:58 AM UTC, comment #3: 

Matlab does this:


>> ismember (98, 'abc')
ans =
  logical
   1
>> ismember ('abc', 98)
ans =
  1x3 logical array
   0   1   0


Guillaume <gyom>
Fri 14 Dec 2018 01:26:41 AM UTC, comment #2: 

@Guillaume: does Matlab always promote to numeric?  Or does it depend on whether the input is the first or the second?

What does Matlab return for


ismember (98, 'abc')
ismember ('abc', 98)



Rik <rik5>
Group administrator
Fri 18 May 2018 11:48:42 AM UTC, comment #1: 

It seems that when one input is numeric and the other is a char array, Matlab converts the latter into numeric so I suggest the following fix:


--- a/scripts/set/ismember.m    Sun Sep 04 16:09:43 2016 +0100
+++ b/scripts/set/ismember.m    Fri May 18 12:45:08 2018 +0100
@@ -108,6 +108,13 @@
     a = {a};
   endif

+  ## Another Matlab-compatible behavior. See bug #53924.
+  if (isnumeric (a) && ischar (s))
+    s = double (s);
+  elseif (ischar (a) && isnumeric (s))
+    a = double (a);
+  endif
+
   [a, s] = validsetargs ("ismember", a, s, varargin{:});

   by_rows = nargin == 3;
@@ -165,12 +172,15 @@
 %!assert (ismember ("abc", {"abc", "def"}), true)
 %!assert (isempty (ismember ([], [1, 2])), true)
 %!assert (isempty (ismember ({}, {'a', 'b'})), true)
+%!assert (isempty (ismember ([], 'a')), true)
 %!assert (ismember ("", {"abc", "def"}), false)
+%!assert (ismember (1, 'abc'), false)
+%!assert (ismember ('abc', 1), [false false false])
+%!assert (ismember ('abc', 99), [false false true])
 %!fail ("ismember ([], {1, 2})")
 %!fail ("ismember ({[]}, {1, 2})")
 %!fail ("ismember ({}, {1, 2})")
 %!fail ("ismember ({1}, {'1', '2'})")
-%!fail ("ismember (1, 'abc')")
 %!fail ("ismember ({'1'}, {'1' '2'},'rows')")
 %!fail ("ismember ([1 2 3], [5 4 3 1], 'rows')")
 %!assert (ismember ({"foo", "bar"}, {"foobar"}), [false false])


Guillaume <gyom>
Wed 16 May 2018 11:03:42 AM UTC, original submission:  

Unusual corner case with ismember() but it highlights a difference between Octave and Matlab.

In Octave:


octave> ismember([],'a')
error: Invalid call to lookup.  Correct usage is:
 -- IDX = lookup (TABLE, Y)
 -- IDX = lookup (TABLE, Y, OPT)
error: called from
    print_usage at line 91 column 5
    ismember at line 136 column 10


In Matlab:


>> ismember([],'a')
ans =
  0x0 empty logical array


Testing further, one also gets in Octave:


octave> ismember([],{'a'})
ans = [](0x0)


while Matlab gives an error:


>> ismember([],{'a'})
Error using cell/ismember (line 34)
Input A of class double and input B of class cell must be cell arrays of character vectors, unless one is a
character vector.


Guillaume <gyom>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by gyom (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2018-12-14 rik5 StatusPatch Submitted Fixed
        Open/ClosedOpen Closed
    2018-12-14 rik5 StatusNone Patch Submitted

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code