How to use && function for two ismembers on one line

I am trying to use two isembers on one line but I am running into a bug, how can I go around this bug? Please see attached picture. Any guidance will be much appreaciated

 Accepted Answer

The first output of ismember() is an array the same size as the first parameter to ismember(), with the output indicating whether the corresponding input value is found in the second parameter to ismember()
Your B is a vector, so ismember(B,A) is going to return a vector. You then compare that vector to the value 1. When you compare an array to a scalar, you get an output that is the same size as the array. So you have a vector value on the left side of the && but && is reserved for scalars on both sides of it.
Your disp() message hints you wanted to test ismember(2, A) .
Note that your else would activate if either side of the && was false -- if 2 is not a member of A then false, if 4 is not a member of A then false. But the disp message you display is for the case that 2 is not a member and 4 is not a member. Suppose that 2 is not a member but 4 is a member, then the && would be considered false even if 4 is a member of A.

7 Comments

I am not really concerned with the else function at the moment, you'd say it's irrevelant to the code that I am working on. Just pretend the else-statement and it's line of code aren't there.
I understand that and-operators (&&) are used for scalars, how do you then suggest I go around this?
if all(ismember(B,A)) && ismember(4,A)
or
if any(ismember(B,A)) & ismember(4,A)
depending what you are trying to do
Thank you, that helped!
I want to include the (&&) operator, but I am running into a bug when I use it. The bug is from the fact that I am having vectors on both sides of (&&) operator instead of scalars. What I am asking is that, if I want to use ismember on one line for 2 or 3 vectors on either side of (&&), how can I do it? See code below.
A = [2 3 4 5 6];
B = [1 7 8 9 10];
C = B(1,1:end);
D = 1;
if (ismember(B, A)) && (ismember(C, A))
disp(';')
elseif all(ismember(B,A)) && not(ismember(D,A))
disp('')
else
disp('-_-')
end
Error: Operands to the logical and (&&) and or (||) operators must be convertible to logical scalar
values.
Error in (line 9)
if (ismember(B, A)) && (ismember(C, A))
In any case where you use the && operator, then both sides of the operator must be scalars.
In some cases it is meaningful to reduce vector arguments to scalar arguments using the all() or any() operations.
I cannot tell from your code what you are trying to do -- especially since your B and C are certain to be the same as long as B is a row vector, so that part seems pointless.
In my code, I am testing 4 conditions. The first one is where the members of C and B are both in A, the second one is where only the members of C are in A, and the members of B aren't in A, the third condition is where the membes of C are not in A, and the members of B are in A, the fourth condition is where neither the members of C or B are in A. I've attached a code which is very similar to what I am working on, I hope it makes sense. The code below illustrates what I am trying to do.
A = [2 3 4 5 6];
B = [1 7 8 9 10];
C = [8 9 1];
if all(ismember(B, A)) && all(ismember(C,A))
disp('B and C are subsets of A')
elseif not(ismember(B, A)) && all(ismember(C,A))
disp('C is the only subset of A')
elseif all(ismember(B, A)) && not(ismember(C,A))
disp('B is the only subset of A')
else
disp('neither B nor C are subsets of A')
end
You have vectors. You need more cases for where some of the elements are in one vector but not the other vector.
elseif not(ismember(B, A)) && all(ismember(C,A))
Caution: the not() operation is element-by-element, so if there is more than one element in B, not(ismember(B,A)) is a vector, and that will fail on &&
Is it possible that what you are expected to do is proceed element-by-element and produce a per-element output? That might, for example, look something like ';;- ;_' ?

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!