How do I find the rows in one table that have strings that match strings in another table?
54 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Brian Yellen
le 10 Fév 2021
Commenté : Brian Yellen
le 12 Fév 2021
I have two tables, both with a column labeled core_id. I would like to know which rows in table A have a core_id string that matches any core_id string in table B.
I made this example, where variable B has 'bb' and 'dd' in common with variable A. The index gives the correct positions, 2 and 7:
>> A={'aa' 'bb' 'tt' 'yy' 'uu' 'cc' 'dd'};
B={'zz' 'bb' 'dd' 'gg' 'jj'};
ind=find(contains(A,string(B)))
ind =
2 7
However, when I try it with my tables, I cannot get the indeces of the rows with matching string data in column 'core_id' -- I just get a
DS = readtable('blahA');
NE = readtable('blahB');
dps_ind=find(contains(DS.core_id,string(NE.core_id)));
Instead of giving me the indices, dps_ind contains the string data for the cells for which I just want the indices. Why is this code working differently from the simple example above? And how do I fix it?
2 commentaires
Jakob B. Nielsen
le 11 Fév 2021
Can you show an example of what your tables look like? The default output of the find function should be indices, so your example should give you what you want... There is up to three outputs of find one of which is the data itself. Have you tried [row,col,v]=find(...) and then taken out row? I dont know why this would happen with your example, but it is the only thing I can think of.
Réponse acceptée
Adam Danz
le 12 Fév 2021
Use this line to find the rows in NE.study_id that are in DS.study_id.
ismember(NE.study_id, DS.study_id)
If you want to ignore case, use
ismember(lower(___), lower(___)); % or upper
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical 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!