Find numbers that are not in multiple columns
Show older comments
I have three tables:
A1 =
5×2 table
x Var1
__ ____
1 2524
2 2342
3 4353
4 4353
5 2348
A2 =
5×2 table
x Var2
__ ____
1 6745
3 5942
4 9569
5 2454
6 1240
A3 =
5×2 table
x Var3
__ _____
2 4252
3 9345
4 9235
5 1246
6 3965
For each table I want to find and delete the rows that contains a number in the first row that is not in the first rows of both other tables. So I want to end up with this:
A1 =
3×2 table
x Var1
__ ____
3 4353
4 4353
5 2348
3×2 table
x Var2
__ ____
3 5942
4 9569
5 2454
A3 =
3×2 table
x Var3
__ ____
3 9345
4 9235
5 1246
How can I do this? Preferably with no loops.
4 Comments
> For each table I want to find and delete the rows that contains a number in the first row that is not in the first rows of both other tables.
Based on the examples you provided (which are helpful to explain the goal, thanks!), it sounds like you want to delete any rows that contain a number in column 1 that is not in the first row, column 1 of the other tables. Otherwise, why is the first row of A3 deleted?
Dylan den Hartog
on 19 May 2021
Siddharth Bhutiya
on 19 May 2021
I believe this is just a dummy example, but is there a reason you have a table with 1 two column variable instead of creating a table with 2 variables? The description of your problem makes it seem that the two columns of your Var1 are separate thing (they just seem to have the same data type).
Dylan den Hartog
on 19 May 2021
Edited: Dylan den Hartog
on 19 May 2021
Accepted Answer
More Answers (1)
Sulaymon Eshkabilov
on 19 May 2021
Edited: Sulaymon Eshkabilov
on 19 May 2021
Simle solution is a logical indexing approach, something like ...e.g.:
IND = A1.Var1~=A2.Var2;
A1=array2table([A1.x(IND), A1.Var1(IND)], 'variablenames', {'x', 'Var1'});
...
1 Comment
Adam Danz
on 19 May 2021
This does an ordered-comparison. Notice that the first row of table A3 should be removed but none of the other tables contains a 2 in the first row of column 1.
You'd need ismember if I understand the OP correctly.
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!