查找哪个字母出现次数最多 [英] Finding which letter has maximal occurence
本文介绍了查找哪个字母出现次数最多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试了Matlab和网络来寻找答案,但徒劳无功,所以我需要您的帮助 我使用下面的代码来查找数组中字母的出现次数;
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)] `
结果=
'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?
推荐答案
索引是函数max
的第二个输出.
The index is the second output of the function max
.
所以您应该这样做:
[~,index]=max(counts)
mostCommonLetter=matchCell{index};
这篇关于查找哪个字母出现次数最多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文