How to find common rows between two cell arrays containing string values?

I have two cell arrays A and B containing string values of the form like this :
A = (ABC , DEF ;
HTG , JUKHI;
RTHG,KIO)
B = (HTG , JUKHI;
GHTY , UJIK;
RTGHY, IOP)
I want to find the common rows from these cell arrays, i.e., output = [HTG , JUKHI]
How to do that? I have tried ismember, but it doesnot give correct ans because I want to match exact rows and not just one entry in a row. Any solutions?

1 件のコメント

Jan
Jan 2019 年 4 月 11 日
This is no valid Matlab code to create "string values". Therefore the readers have to guess, what your inputs are. So please post some code, which creates the inputs.

サインインしてコメントする。

回答 (2 件)

Jan
Jan 2019 年 4 月 11 日
編集済み: Jan 2019 年 4 月 11 日
What about ismember(A, B, 'rows')?
I guess your inputs are:
A = ["ABC", "DEF"; ...
"HTG", "JUKHI"; ...
"RTHG", "KIO"];
B = ["HTG", "JUKHI"; ...
"GHTY", "UJIK"; ...
"RTGHY", "IOP"]
index = ismember(A, B, 'rows');
result = A(index, :)
or:
result = intersect(A, B, 'rows')

2 件のコメント

Deepika Vatsa
Deepika Vatsa 2019 年 4 月 11 日
'rows' argument does not go with cell arrays in ismember
Jan
Jan 2019 年 4 月 12 日
@Deepika Vatsa: You did not mention, that you are talking of cell string. I asked you in a comment, what your inputs are and you did not reply to my comment. It wastes time to let the readers guess, what your inputs are.
The code I have posted works as expected. So simply use strings instead of cell strings.
A = {'ABC', 'DEF'; ...
'HTG', 'JUKHI'; ...
'RTHG', 'KIO'};
B = {'HTG', 'JUKHI'; ...
'GHTY', 'UJIK'; ...
'RTGHY', 'IOP'};
[~, iA] = intersect(strcat(A(:,1), '*', A(:,2)), ...
strcat(B(:,1), '*', B(:,2)));
Result = A(iA, :);

サインインしてコメントする。

Alex Mcaulley
Alex Mcaulley 2019 年 4 月 11 日
An option:
A = {'ABC','DEF';'HTG','JUKI';'RTHG','KIO'};
B = {'HTG','JUKI';'GHTY','UJIK';'RTGHY','IOP'};
setdiff(A,setdiff(A,B))

6 件のコメント

Alex Mcaulley
Alex Mcaulley 2019 年 4 月 11 日
Or:
A = {'ABC','DEF';'HTG','JUKI';'RTHG','KIO'};
B = {'HTG','JUKI';'GHTY','UJIK';'RTGHY','IOP'};
intersect(A,B)
Deepika Vatsa
Deepika Vatsa 2019 年 4 月 11 日
setdiff also does not take each row as a single entity as required in this problem
Alex Mcaulley
Alex Mcaulley 2019 年 4 月 11 日
編集済み: Alex Mcaulley 2019 年 4 月 11 日
Ohhh sorry I haven't understood. I think this is a robust solution:
A = {'ABC','DEF';'HTG','JUKI';'RTHG','KIO'};
B = {'HTG','JUKI';'GHTY','UJIK';'RTGHY','IOP'};
try
res = A(ismember(strcat(A(:,1),A(:,2)),intersect(strcat(A(:,1),A(:,2)),strcat(B(:,1),B(:,2)))),:);
catch
res = [];
end
Deepika Vatsa
Deepika Vatsa 2019 年 4 月 11 日
編集済み: Deepika Vatsa 2019 年 4 月 11 日
yeah this works! Thanks. BTW I got my solution using ismember function and using indexes like this:
[~ , idd1] = ismember(A(:,1),B(:,1));
[~ , idd2] = ismember(A(:,2),B(:,2));
idx = idd1 == idd2 & idd1 > 0 & idd2 > 0;
common_rows = A(idx,:);
Logical indexes just gives confusing answer. But your solution also works.
Alex Mcaulley
Alex Mcaulley 2019 年 4 月 11 日
But your code doesn't work in your first example:
[~ , idd1] = ismember(A(:,1),B(:,1));
[~ , idd2] = ismember(A(:,2),B(:,2));
idx = idd1 == idd2;
common_rows = A(idx,:);
common_rows =
3×2 cell array
'ABC' 'DEF'
'HTG' 'JUKI'
'RTHG' 'KIO'
Deepika Vatsa
Deepika Vatsa 2019 年 4 月 11 日
Yeah just figured out that. Edited my answer above.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

質問済み:

2019 年 4 月 11 日

コメント済み:

Jan
2019 年 4 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by