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

7 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by