how to match and eliminate those values??

조회 수: 3 (최근 30일)
Panda Girl
Panda Girl 2018년 12월 7일
댓글: Panda Girl 2018년 12월 8일
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
Cris LaPierre
Cris LaPierre 2018년 12월 7일
I've formatted your code.
Panda Girl
Panda Girl 2018년 12월 8일
thank you so much @CrisLapierre
@ImageAnalyst Noted

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2018년 12월 7일
편집: Stephen23 2018년 12월 7일
"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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by