2

I tried Matlab and the net to find an answer but in vain so I need your help I have used the code below to find number of occurrences of the letters in an array;

characterCell = {'a' 'b' 'b' 'a' 'b' 'd' 'c' 'c'};  %# Sample cell array 
matchCell = {'a' 'b' 'c' 'd' 'e'};                  %# Letters to count      
[~,index] = ismember(characterCell,matchCell);  %# Find indices in matchCell 
counts = accumarray(index(:),1,[numel(matchCell) 1]);  %# Accumulate indices 
results = [matchCell(:) num2cell(counts)] `

results =

'a'    [2] 
'b'    [3] 
'c'    [2] 
'd'    [1] 
'e'    [0] 

Now I need to get which letter has the highest occurrence How to know the index?

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
pac
  • 291
  • 9
  • 19

2 Answers2

3

The mode function tells you the most frequent value.

mostCommonLetter = mode(matchCell[:]);
David Cary
  • 5,250
  • 6
  • 53
  • 66
2

The index is the second output of the function max.

So you should do:

[~,index]=max(counts)
mostCommonLetter=matchCell{index};
Oli
  • 15,935
  • 7
  • 50
  • 66