Compare two tables, show same elements with correct id

4 次查看(过去 30 天)
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.

回答(1 个)

Dev
Dev 2025-3-28
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.

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by