How to use && function for two ismembers on one line
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Andromeda
am 24 Mär. 2022
Kommentiert: Walter Roberson
am 26 Mär. 2022
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/940574/image.jpeg)
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 24 Mär. 2022
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 Kommentare
Walter Roberson
am 26 Mär. 2022
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 ';;- ;_' ?
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Type Identification finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!