How do I find the rows in one table that have strings that match strings in another table?
56 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jakob B. Nielsen
2021-2-11
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.
采纳的回答
Adam Danz
2021-2-12
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!