MATLAB Answers

0

how to match and eliminate those values??

Asked by Panda Girl on 7 Dec 2018 at 19:34
Latest activity Commented on by Panda Girl on 8 Dec 2018 at 20:48
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

  3 Comments

Please format code as code, not as a bulleted list. That way, we can click the copy button and paste it right away into MATLAB.
I've formatted your code.
thank you so much @CrisLapierre
@ImageAnalyst Noted

Sign in to comment.

1 Answer

Answer by Stephen Cobeldick on 7 Dec 2018 at 21:42
Edited by Stephen Cobeldick on 7 Dec 2018 at 22:25
 Accepted Answer

"i am not able to understand this"
Your code:
find(codes~=s))
first creates a logical array with the same size as codes, with 1 in every location where the arrays differ (you can see that the second row is all zero, where the data match exactly):
>> codes~=s
ans =
0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0
You then use find to obtain the linear indices of those 1's. From the above matrix it is clear that the linear indices will be [6,7,10,12,...], but this is easy to check using MATLAB too:
>> find(codes~=s)
ans =
6
7
10
12
18
19
22
24
25
27
28
33
37
39
40
45
You then use these linear indices to select elements from codes, you can check this yourself too:
codes(6) = 0
codes(7) = 1
codes(10) = 1
codes(11) = 1
codes(12) = 1
... etc
codes(40) = 0
codes(45) = 1
and that is your output vector. Interesting, but ultimately not very useful for the task you described.
"how to match and eliminate those values??"
So far two people have advised to use setdiff: why are you not doing this?
Below is a copy of my answer to your earlier question , where I showed you two ways to achieve what you want:
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

  1 Comment

Thank you so much @Stephen Cobeldick
You are great

Sign in to comment.