Borrar filtros
Borrar filtros

How to compare two matrix?

2 visualizaciones (últimos 30 días)
Md Jahid Hasan Sagor
Md Jahid Hasan Sagor el 26 de Abr. de 2023
Comentada: Md Jahid Hasan Sagor el 27 de Abr. de 2023
Suppose,
A=[4 5;4 19;5 7;4 5];
B=[4 5]
if B in A
calculation1;
end
else
calculation2;
end
How Can I code this?

Respuesta aceptada

DGM
DGM el 26 de Abr. de 2023
Editada: DGM el 26 de Abr. de 2023
It's not exactly clear what the intended logic is, but this is a simple membership test
A=[4 5; 4 19; 5 7; 4 5];
B=[4 5];
if ismember(B,A,'rows')
% calculation 1;
disp('B is a member of A')
else
% calculation 2;
disp('B is NOT a member of A')
end
B is a member of A
  1 comentario
Md Jahid Hasan Sagor
Md Jahid Hasan Sagor el 27 de Abr. de 2023
Thank you so much for your help. It works.

Iniciar sesión para comentar.

Más respuestas (1)

Juan Ruiz Osorio
Juan Ruiz Osorio el 26 de Abr. de 2023
Editada: Juan Ruiz Osorio el 26 de Abr. de 2023
I think this works if you want to do a calculation for each member of B.
A=[4 5;4 19;5 7;4 5];
B=[4 5];
for i=1:size(B,2)
if ismember(B(i),A)
calculation1;
else
calculation2;
end
end

Categorías

Más información sobre Dynamic System Models 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