MATLAB: Number of times a particular element appears in a cell array

cellcell array

If I have an mxn cell array whose elements are either an integer or a vector of integers, how could I count how many times a particular integer occurs in the entire cell array? The integer could occur individually in a cell or in a vector in a cell.

Best Answer

Without seeing your cell array (the structure of the cell array is important), I can only guess.
For this array (likely an example of the most complicated), this code will do what you want:
celary = { {randi(9, 5, 1)}, {randi(20, 1, 50)}, {randi(99, 20, 1)} }; % Cell Array (Obviously!)
nr_in_cell = cellfun(@(x) find(x{:}==5), celary, 'Uni',0); % Find OccurrencesIn Each Cell
total_occurrences = numel([nr_in_cell{:}]); % Output Total Occurrences In All Cells