2012-02-24 23 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の2番目の出力です。

だから、あなたが行う必要があります。

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

function maxの最初の出力は何ですか? – pac

+0

これは最大値です。 – Oli

3

mode functionあなたが最も頻繁に価値を伝えます。

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

この行に何か間違っています。申し訳ありませんが動作していません – pac

関連する問題