syntax to used unmatched values in matlab?
2 views (last 30 days)
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?
0 Comments
Accepted Answer
madhan ravi
on 7 Dec 2018
idx=ismember(c,a); % idx is logical index
c(~idx)*4 %multiply with whatever number you wish
0 Comments
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
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.
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
0 Comments
See Also
Categories
Find more on Operators and Elementary Operations 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!