How can I see if one cell also exists in another cell array?

5 ビュー (過去 30 日間)
Luca Kolibius
Luca Kolibius 2016 年 9 月 19 日
編集済み: Stephen23 2016 年 9 月 19 日
Hello Matlab Community,
I have one 640x1 cell array (some cells are empty) and one 1280x1 cell array. Now, I would like to know how many of the words in the 640x1 cell array also appear in the 1280x1 cell array. Inside the cells are words ordered from Z-A. I have already played around with "==", "isequal" and "strcmp", but could not find a solution that works. In Excel I have solved it with "=SUMPRODUCT(COUNTIF(A2:A?;B2:B?). This always required to select the relevant filled cells and I would like to move the whole analysis over to MATLAB.
Is there a script that can do that, or do I have to stick to Excel? Is would be superb if that script would also ignore empty cells, so I do not have to select the cell array each time.
All the best, Luca

回答 (2 件)

KSSV
KSSV 2016 年 9 月 19 日
You can remove the empty cells using :
A = A(~cellfun('isempty',A)) ; whre A is your cell array
Two compare the two cell arrays you can use functions like setdiff, intersect in MATLAB.

Stephen23
Stephen23 2016 年 9 月 19 日
編集済み: Stephen23 2016 年 9 月 19 日
Have a look at this simple example, which shows how many strings of A are in B:
>> A = {'cat','in','the','hat'};
>> B = {'cat','fat','hat','rat','sat','tat'};
>> nnz(ismember(A,B))
ans = 2

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by