Borrar filtros
Borrar filtros

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

3 visualizaciones (últimos 30 días)
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 comentarios
TADA
TADA el 24 de Ag. de 2021
Editada: TADA el 24 de Ag. de 2021

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 el 24 de Ag. de 2021
Also, does Wan Jis answer solve your problem?

Iniciar sesión para comentar.

Respuestas (1)

Wan Ji
Wan Ji el 24 de Ag. de 2021
Editada: Wan Ji el 24 de Ag. de 2021
% 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 comentario
Wan Ji
Wan Ji el 24 de Ag. de 2021
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

Iniciar sesión para comentar.

Categorías

Más información sobre Sparse Matrices 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