deleting rows using setdiff?
조회 수: 7 (최근 30일)
이전 댓글 표시
I have two different matrices, x & x1, and they are related. x is generated to be compared to y, all elements that are not availbe in y need to be ignored in the output matrix. So, the output matrix Tx has the rows of x1 that are results of comparison between x and y.
I used this code, but couldn't figure out how to delete the rows using the idx results;
x1=[1 0 0;0 1 1;1 0 2;2 0 1;2 2 2];
% x & x1 are related.
x=[2;1;5;6;8];
y=[1;2;5;6]; % wanted values of x1
[z,idx]=setdiff(x,y,'rows')
%Tx should return the values of x1
% based on the comparison matrix z
% whih contains unwanted rows
Tx=setdiff(x1,[idx],'rows','stable');
the output is expexted to be Tx=[1 0 0;0 1 1;1 0 2;2 0 1]; %row5 is deleted
댓글 수: 0
답변 (1개)
dpb
2016년 9월 26일
For what you're looking for, ismember is better than setdiff --
x=[2;1;5;6;8];
y=[1;2;5;6]; % wanted values of x1
>> Tx=x1(ismember(x,y),:)
Tx =
1 0 0
0 1 1
1 0 2
2 0 1
>>
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!