syntax to used unmatched values in matlab?

c = [1,4,3]
sci = 1
a = sci;
findsamemember = ismember[c,a]
output will be
1 0 0
I am trying to get the values that are not matching with a in further process of the program. That is, the value that should be used as output in my next syntax is 4 and 3 (or at position 2 and 3)
How should I proceed with it?

 Accepted Answer

idx=ismember(c,a); % idx is logical index
c(~idx)*4 %multiply with whatever number you wish

More Answers (2)

Simply use the function MEANT for this: setdiff():
c = [1,4,3]
sci = 1
a = sci;
[nonMatchingValues, nonMatchingIndexes] = setdiff(c, a)

5 Comments

Panda Girl
Panda Girl on 7 Dec 2018
Edited: Stephen23 on 7 Dec 2018
I am trying to work the above example with this example
>> s = sci_new
s =
1 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0
>> codes
codes =
1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0
1 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0
1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0
z1 = codes(find(codes~=s))
I am trying to match the values of s and codes. theoritically the values present in s matches with row 2 from codes I want to eliminate that row and consider the other rows but z1 is giving me
z1 =
0
1
1
1
1
0
0
0
1
1
1
0
0
0
0
1
i am not able to understand this. Kindly help me
I didn't find anything like that at all in my code. I didn't use find()? Why did you not take my suggestion of using setdiff()?
It looks like Stephen also took your example and used setdiff() on it. If you won't take our solutions, there's not much more we can do.
@Image Analyst My apologies if you got offended for not using your solutions. But, I am a begineer at matlab.. when you used setdiff in above example with matching and non matching indexes. I was not able to understand how should I use with my binary example. You might be an expert at this but everyone here is not... some people take time to understand and then implement your solutions.
Thanks anyways for your help.
If you used my code,
c = [1,4,3]
sci = 1
a = sci;
[nonMatchingValues, nonMatchingIndexes] = setdiff(c, a)
You'll see that it produces:
nonMatchingValues =
3 4
nonMatchingIndexes =
3
2
which means that 3 doesn't match and is found at index 3, and 4 doesn't match and is found at index 2.
This is exactly what you said you wanted so all you had to do was copy and paste.
Note that the answer you accepted
idx=ismember(c,a); % idx is logical index
c(~idx)*4 %multiply with whatever number you wish
gives
ans =
16 12
which is not what you said you wanted. You did not want 16 and 12.
Now, for your next question, the approach I'd take is completely different
since both 0 and 1 appear in both of your arrays, so we can't use setdiff()
We need to use ismember instead, with the rows option. Using that
we can find out that row 2 is a match, and rows 1 and 3 do not match.
Here is the code:
s = [1 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0]
codes = [...
1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0
1 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0
1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0]
matchingRows = ismember(codes, s, 'rows')
nonMatchingRows = find(~matchingRows)
You'll see that it produces:
matchingRows =
3×1 logical array
0
1
0
nonMatchingRows =
1
3
I think that is what you want, right? If so, copy it and try it. If not, explain better - we're still willing to help you.
@Panda Girl: I've removed your flag. I suggest not to take the answers personally. Sometimes the very active members in internet forums behave, like they have answered a question a hundred times before, because they have answered it a hundred times before. It would be better, if all discussions are quite, calm and polite, but we are human.
I'm convinced, that Image Analyst was not offended by your question, and that you do not have to be offended, if he repeats to suggest using the alraedy provided solution. All he wants to do is to solve the problem.

Sign in to comment.

Stephen23
Stephen23 on 7 Dec 2018
Edited: Stephen23 on 7 Dec 2018
Method one: setdiff:
>> s = [1,1,0,0,0,0,1,1,0,0,1,1,1,1,0,0]
s =
1 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0
>> c = [1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0;1,1,0,0,0,0,1,1,0,0,1,1,1,1,0,0;1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0]
c =
1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0
1 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0
1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0
>> z = setdiff(c,s,'rows')
z =
1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0
1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0
Note that setdiff can change the order of the rows, unless you use the 'stable' option:
setdiff(c,s,'stable','rows')
Method two: indexing:
>> x = all(s==c,2);
>> z = c(~x,:)
z =
1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0
1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0

Asked:

on 7 Dec 2018

Commented:

Jan
on 9 Dec 2018

Community Treasure Hunt

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

Start Hunting!