フィルターのクリア

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 Center および File ExchangeSparse Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by