Compare two tables, show same elements with correct id

4 visualizaciones (últimos 30 días)
Miki Arswark
Miki Arswark el 7 de Abr. de 2021
Respondida: Dev el 28 de Mzo. de 2025
Hello, I have 3 tables, first is 'id' second T1 and third T2 have random logical numbers (0,1).
I want to compare T1 and T2, show id when row of T1 and T2 have logical 1.
I tryed everything even ismember but it dont work - it shows all logical 1 of first table only.
Can someone help me ? Im new in Matlab.

Respuestas (1)

Dev
Dev el 28 de Mzo. de 2025
In order to show the ‘id’ whenever ‘T1’ and ‘T2’ both have a logical 1, we can leverage the following steps-
  • Create a logical array where each element is true if the corresponding element in ‘T1’ is 1 and create another logical array where each element is true if the corresponding element in ‘T2’ is 1.
  • Perform an element-wise logical AND operation between these two logical arrays and store the result as a logical array where each element is true if both ‘T1’ and ‘T2’ have a value of 1 at that position.
I have also added a reference code line which performs the above two steps combinedly-
% Find indices where both ‘T1’ and ‘T2’ have logical 1
matchingIndices = (T1 == 1) & (T2 == 1);
  • Next, use logical indexing to extract the IDs from the ‘id’ table where ‘matchingIndices’ is true. This filters the id array to include only those IDs where both ‘T1’ and ‘T2’ have logical 1.
Below is a reference code which performs the same-
% Extract the corresponding IDs
matchingIDs = id(matchingIndices);
  • Finally, display ‘matchingIDs’ which shows the ID when a row of ‘T1’ and ‘T2’ have logical 1.
I hope this solves the question.

Categorías

Más información sobre Tables en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by