syntax to used unmatched values in matlab?

2 次查看(过去 30 天)
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?

采纳的回答

madhan ravi
madhan ravi 2018-12-7
idx=ismember(c,a); % idx is logical index
c(~idx)*4 %multiply with whatever number you wish

更多回答(2 个)

Image Analyst
Image Analyst 2018-12-7
Simply use the function MEANT for this: setdiff():
c = [1,4,3]
sci = 1
a = sci;
[nonMatchingValues, nonMatchingIndexes] = setdiff(c, a)
  5 个评论
Image Analyst
Image Analyst 2018-12-8
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
Jan 2018-12-9
@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
Stephen23 2018-12-7
编辑:Stephen23 2018-12-7
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

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by