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
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.
Brian Yellen
Brian Yellen 2021-2-11
Hi Jakob,
Thanks for your reply. I tried your suggestion [row,col,v]=find(...), but matlab said "Too manu output arguments" ... and said the same thing when I tried two outputs.
Here is what the data tables look like.
I have 101 unique core_id strings in table NE
I want to find the indices of 1194 rows that have these core_id strings in table DS
Thanks again for your help!
Brian

请先登录,再进行评论。

采纳的回答

Adam Danz
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 CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by