drop same row from one matrix based on another matrix

조회 수: 5 (최근 30일)
zhengyang shang
zhengyang shang 2021년 3월 25일
댓글: Walter Roberson 2021년 3월 26일
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?

답변 (1개)

Walter Roberson
Walter Roberson 2021년 3월 25일
c = a;
c(ismember(c(:,1), b),:) = [];
  댓글 수: 2
zhengyang shang
zhengyang shang 2021년 3월 25일
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 2021년 3월 26일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Marine and Underwater Vehicles에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by