Intersection of two tables
17 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Vilém Frynta
le 24 Fév 2023
Commenté : Vilém Frynta
le 24 Fév 2023
Hi,
I'm trying to intersect two tables. They share same column with strings, and I want strings that are in both of these tables (intersection).
Example tables:
A = table();
A.user = ["user1";"user2";"user3";"user4"];
A.value = [10;20;30;40]
B = table();
B.user = ["user3";"user4";"user5"];
B.value = [300;400;500]
I would like to get a logical vector (index) which would tell me positions of users, that are in both tables, which are user3 and user4 in this example. Values of the users are not important here, i need only those indexes.
Although this seems simple in my head, I wasn't able to do it in Matlab. I tried using outerjoin(), ismember() and intersect(), but always some errors and empty logical vectors as results.
0 commentaires
Réponse acceptée
Les Beckham
le 24 Fév 2023
Modifié(e) : Les Beckham
le 24 Fév 2023
A = table();
A.user = ["user1";"user2";"user3";"user4"];
A.value = [10;20;30;40]
B = table();
B.user = ["user3";"user4";"user5"];
B.value = [300;400;500]
Since you say you only care about the indices but you didn't say which table you wanted them for, here are both.
idxBinA = ismember(B.user, A.user) % where members of A.user are found in B.user
idxAinB = ismember(A.user, B.user) % where members of B.user are found in A.user
Does that get you what you need?
Plus de réponses (1)
Askic V
le 24 Fév 2023
Modifié(e) : Askic V
le 24 Fév 2023
Maybe this code can help you:
A = table();
A.user = {'user1';'user2';'user3';'user4'};
A.value = [10;20;30;40]
B = table();
B.user = {'user3';'user4';'user5'};
B.value = [300;400;500]
ind = ismember(B.user, A.user)
A = table();
A.user = {"user1";"user2";"user3";"user4"};
A.value = [10;20;30;40]
B = table();
B.user = {"user3";"user4";"user5"};
B.value = [300;400;500]
idxBinA = ismember([B.user{:}], [A.user{:}])
3 commentaires
Voir également
Catégories
En savoir plus sur Environment and Settings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!