2012-02-24 29 views
2

我試過Matlab和網絡來找到答案,但徒勞無功,所以我需要你的幫助 我已經使用下面的代碼來查找數組中的字母出現次數;找到哪個字母有最大的出現

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)] ` 

結果=

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

現在我需要得到這封信具有最高發生 如何知道索引?

回答

2

該索引是函數max的第二個輸出。

所以,你應該做的:

[~,index]=max(counts) 
mostCommonLetter=matchCell{index}; 
+0

函數max的第一個輸出是什麼? – pac 2012-02-24 21:26:01

+0

這是最大值。 – Oli 2012-02-24 21:26:25

3

mode function告訴你最頻繁的值。

mostCommonLetter = mode(matchCell[:]); 
+0

這一行有問題。對不起,它不工作 – pac 2012-02-25 11:07:39

相關問題