deleting rows using setdiff?

14 vues (au cours des 30 derniers jours)
JacobM
JacobM le 26 Sep 2016
Réponse apportée : dpb le 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

Réponses (1)

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

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by