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

103 visualizaciones (últimos 30 días)
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 comentarios
Jakob B. Nielsen
Jakob B. Nielsen el 11 de Feb. de 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.
Brian Yellen
Brian Yellen el 11 de Feb. de 2021
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

Iniciar sesión para comentar.

Respuesta aceptada

Adam Danz
Adam Danz el 12 de Feb. de 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

Más respuestas (0)

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by