deleting rows using setdiff?

조회 수: 7 (최근 30일)
JacobM
JacobM 2016년 9월 26일
답변: dpb 2016년 9월 26일
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

답변 (1개)

dpb
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
>>

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by