drop same row from one matrix based on another matrix

5 visualizaciones (últimos 30 días)
zhengyang shang
zhengyang shang el 25 de Mzo. de 2021
Comentada: Walter Roberson el 26 de Mzo. de 2021
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?

Respuestas (1)

Walter Roberson
Walter Roberson el 25 de Mzo. de 2021
c = a;
c(ismember(c(:,1), b),:) = [];
  2 comentarios
zhengyang shang
zhengyang shang el 25 de Mzo. de 2021
i am sorry it seems not ture. because it just drop the last 30 rows of a but not contain b
Walter Roberson
Walter Roberson el 26 de Mzo. de 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,:)
ans = 10×2
65 4 39 8 45 2 69 1 66 4 55 5 59 6 90 7 4 1 51 6
%let us get an idea of what is in b
b(1:10,:)
ans = 10×1
45 1 27 85 96 42 37 43 22 34
%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)
ans = 1×2
253 2
ans = 1×2
30 1
ans = 1×2
223 2
%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,:)
ans = 10×2
65 4 39 8 69 1 66 4 55 5 59 6 90 7 4 1 51 6 28 0
%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))
ans = 0

Iniciar sesión para comentar.

Categorías

Más información sobre Marine and Underwater Vehicles en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by