필터 지우기
필터 지우기

how to find the find the different rows in 2 matrices based on 2 conditions?

조회 수: 3 (최근 30일)
Sahar Sowdagar
Sahar Sowdagar 2021년 8월 24일
댓글: Wan Ji 2021년 8월 24일
hello! sorry if the question is ridiculous im new to programming.
I have 2 matrices of different sizes, each with 60 columns, but different number of rows.
What I need to do is match up the 2 matrics according to columns 4 and 6, so if col4(A) = col4(B), AND col6(A)=col6(B) then i keep that row, but if for any given row in A, it doesnt match in col4 and col6 with any other row in B, then i delete it.
i used to loop for the whole matrix and use the setdiff function for col4 then for col6, but that takes around 15 minutes to run, so Im wondering if there is a faster way to do this?
If you need to know any extra information, let me know!
  댓글 수: 4
TADA
TADA 2021년 8월 24일
편집: TADA 2021년 8월 24일

I see. I have a few questions,

  • can there be duplicate rows?
  • do BOTH colum 4 and 6 need to be equal?
  • Is the data sorted in any way?
  • does the order of the output make a difference?
TADA
TADA 2021년 8월 24일
Also, does Wan Jis answer solve your problem?

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

답변 (1개)

Wan Ji
Wan Ji 2021년 8월 24일
편집: Wan Ji 2021년 8월 24일
% I just try my best to catch your idea by looking at your words, my engish
% is not so good but I want to help you with this problem.
minRowNum = min(size(A,1), size(B,1)); % get the row number that is smaller between A and B
p = 1:minRowNum; % use p as indices
q = false(size(A,1),1); % q as logical indices
q(p) = A(p,4)~=B(p,4) | A(p,6)~=B(p,6); % compare col 4 and col 6, if not equal, return true then let it be deleted
A(q) = []; % then rows are deleted for matrix A
  댓글 수: 1
Wan Ji
Wan Ji 2021년 8월 24일
Also, as I catch your words, you have used setdiff, does it mean that if the elements of column 4 in A are the member of column 4 in B, as well as the elements of column 6 in A are the member of column 6 in B, then you keep the row, else delete it? IF SO, the way will be
q = ismember(A(:,4),B(:,4)) & ismember(A(:,6),B(:,6)); % compare col 4 and col 6, if equal, return true then keep it
A(~q) = []; % then rows are deleted for matrix A

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

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by