deleting rows using setdiff?

6 Ansichten (letzte 30 Tage)
JacobM
JacobM am 26 Sep. 2016
Beantwortet: dpb am 26 Sep. 2016
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

Antworten (1)

dpb
dpb am 26 Sep. 2016
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
>>

Community Treasure Hunt

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

Start Hunting!

Translated by