How to find out which item is mode of cell array?

2 views (last 30 days)
I have a cell array. I want to find the most repeated item in that cell. for example in this array: {'apple','lemon','apple','peach'} I need a code or function that detects which word('apple') is the most repeated in this cell array. like 'mode' function for numeric arrays.

Accepted Answer

Daniel Shub
Daniel Shub on 22 May 2011
Assuming that all the elements in your array are strings
x = {'apple','lemon','apple','peach'};
y = unique(x);
n = zeros(length(y), 1);
for iy = 1:length(y)
n(iy) = length(find(strcmp(y{iy}, x)));
end
[~, itemp] = max(n);
y(itemp)
If they are not all strings, you can replace strcmp with isequal.

More Answers (0)

Categories

Find more on Data Types in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!