drop same row from one matrix based on another matrix
Afficher commentaires plus anciens
i hava a matrix a(253*2) , the first column is the id number and i want drop the same id in matrix b(30*1) , keep the rest data in matrix c(223*2) how should i do?
Réponses (1)
Walter Roberson
le 25 Mar 2021
c = a;
c(ismember(c(:,1), b),:) = [];
2 commentaires
zhengyang shang
le 25 Mar 2021
Your findings do not agree with my tests.
%test data.
%define a and b so a(:,1) includes all of b exactly once,
%and that the other things in a(:,1) are not in b.
%the order of entries is scrambled so that we can be sure
%that the entries are being removed by value and not by position
b = randperm(99,30).';
not_b = setdiff(1:99,b).';
a1 = [b; not_b(randi(length(not_b), 253-length(b),1))];
a1 = a1(randperm(length(a1)));
a2 = randi([0 9], size(a1));
a = [a1,a2];
%let us get an idea of what is in a
a(1:10,:)
%let us get an idea of what is in b
b(1:10,:)
%now do the task we were asked to do, remove all the entries in a that
%have a column 1 that occurs somewhere in b.
c = a;
c(ismember(c(:,1), b),:) = [];
%check sizes to be sure we got the size wanted
size(a), size(b), size(c)
%check to be sure that some entries in a were removed and that the order of
%the remaining entries remained the same
c(1:10,:)
%check to be sure there are no column 1 entries in c that are the same as
%any entry in b
nnz(ismember(c(:,1),b))
Catégories
En savoir plus sur Marine and Underwater Vehicles dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!