how to find the find the different rows in 2 matrices based on 2 conditions?
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
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
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 Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!