ismember 0×0 empty logical array to logic

조회 수: 27 (최근 30일)
Sonima
Sonima 2019년 4월 18일
댓글: Sonima 2019년 4월 20일
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
else
do that
end
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
madhan ravi
madhan ravi 2019년 4월 19일
First and foremost what is your goal? What is the expected result for given A?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2019년 4월 19일
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
else
do this
end
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
Sonima
Sonima 2019년 4월 19일
편집: Sonima 2019년 4월 19일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

the cyclist
the cyclist 2019년 4월 19일
편집: the cyclist 2019년 4월 19일
Making a huge guess here, but are you sure you don't mean
~ismember(ID,A)
rather than
~ismember(A,ID)
Then, assuming ID is a scalar,
~ismember(ID,[])
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
~ismember(A,ID)
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.
  댓글 수: 3
the cyclist
the cyclist 2019년 4월 19일
OK, so are you aware that
if [true true false]
do this
else
do that
end
will "do that", because every element has to be true to get to the "do this" part of the if statement. (The reason I am so insistent is that this is a very common misunderstanding, and people often think that MATLAB somehow process the vector "in parallel".)
It would be good to give a an example of a combination of A and ID where you expect your if statement to "do this". It seems to me that the only way it happens is if none of the elements of A is equal to ID, in which case there are simpler ways to do what you want.
Sonima
Sonima 2019년 4월 20일
Good point. thanks!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by