フィルターのクリア

find a cell array of strings in another cell array of strings

8 ビュー (過去 30 日間)
sh as
sh as 2016 年 4 月 14 日
コメント済み: Guillaume 2016 年 4 月 14 日
I have two cell array of strings, say A and B. I would like to find all cells in B, in which cells of A are occurred! for example:
A={'aa';'a a';'b a'}
B={'s a';'a a';'ll';'a a';'b a';'h f'}
Here 'a a' happened twice in B, and also 'b a' happened once and 'aa' never happened. Then I would like to delete those cells, so I will have:
B={'s a';'ll';'h f'}
So, I have to consider the repetition of strings like 'a a'.
Does anyone know how to do this?
Many thanks

回答 (2 件)

Guillaume
Guillaume 2016 年 4 月 14 日
ismember is the function that tells you if elements of a set are found in another set.
A = {'aa'; 'a a'; 'b a'}
B = {'s a'; 'a a'; 'll'; 'a a'; 'b a'; 'h f'}
%find which cells of A are in B
isAinB = ismember(A, B);
fprintf('''%s'' of A was found in B\n', A{isAinB});
%find which cells of B are in A and delete them
isBinA = ismember(B, A);
B(isBinA) = []

Jos (10584)
Jos (10584) 2016 年 4 月 14 日
So you want the keep the elements in B that are not in A. This is known is the difference in sets, implemented in matlab like this
A = {'aa' ; 'a a' ; 'b a'}
B = {'s a' ; 'a a' ; 'll' ; 'a a' ; 'b a' ; 'h f'}
OUT = setdiff(B,A)
  2 件のコメント
sh as
sh as 2016 年 4 月 14 日
編集済み: sh as 2016 年 4 月 14 日
Thank you very much for quick reply.
But the problem is that this function also does something like unique function. that it also remove any repetition of other strings in B that are important for me to keep them.
Guillaume
Guillaume 2016 年 4 月 14 日
Then use ismember as per my answer.

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by