MATLAB Answers

ismember 0×0 empty logical array to logic

47 views (last 30 days)
Sonima on 18 Apr 2019
Commented: Sonima on 20 Apr 2019
Hi All,
I have an array "A = [1 2 3 4 5 6]" which its length reduces in a loop
if ~ismember(A,ID)
do this
do that
This works fine until the A=[], which ismember returns
0×0 empty logical array
However I need a Single Value and not logical array. Note that I cannot use any and all functions to reduce Logical Arrays to Single Value, beacuse I faced with other problems when "A" array is not yet empty!
How can I fix this problem?
  1 Comment
madhan ravi
madhan ravi on 19 Apr 2019
First and foremost what is your goal? What is the expected result for given A?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Apr 2019
The obvious answer would seem to be to test
if isempty(A) || ~ismember(A,ID)
If you were hoping there were a hidden preference you could set that would make ismember(A,ID) return a non-empty value without having to change your test.. sorry, there is no such test.
I would point out that you could also rewrite your code:
if ismember(A,ID)
do that
do this
the ismember() would be empty when A is empty, so the test would fail, and do that would not be done, leaving you to fall through to the do this case.
  1 Comment
Sonima on 19 Apr 2019
I tried this already, but it doesn't work!
>> isempty(A) || ~ismember(A,ID)
Operands to the || and && operators must be convertible to logical scalar values.

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 19 Apr 2019
Edited: the cyclist on 19 Apr 2019
Making a huge guess here, but are you sure you don't mean
rather than
Then, assuming ID is a scalar,
returns true, which I'm guessing is the result you are intending.
Like I said ... just a guess. (And you might need to adjust your other logic accordingly.)
Part of the reason I am guessing this is that
returns a vector of values (if A is a vector and ID is a scalar), which is not typically what you want in an if statement. But maybe it is in your case.

Sign in to comment.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!