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

8 views (last 30 days)
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

Answers (2)

KSSV
KSSV on 19 Sep 2016
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 on 19 Sep 2016
Edited: Stephen23 on 19 Sep 2016
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

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!