syntax to used unmatched values in matlab?
Show older comments
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
More Answers (2)
Image Analyst
on 7 Dec 2018
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
on 7 Dec 2018
Edited: Stephen23
on 7 Dec 2018
Image Analyst
on 7 Dec 2018
Edited: Image Analyst
on 8 Dec 2018
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.
Panda Girl
on 8 Dec 2018
Image Analyst
on 8 Dec 2018
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.
Jan
on 9 Dec 2018
@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.
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
Categories
Find more on Time Series Events in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!