How do I find the rows in one table that have strings that match strings in another table?

조회 수: 54 (최근 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