finding most common letter in a cell array
1 次查看(过去 30 天)
显示 更早的评论
If I have a cell array
x={'overcomplete';'phellandrene';'phenanthrene';'pneumatocele';'pneumonocele'
'preintercede';'preinterfere';'pseudocumene';'pseudosphere';'spermatocele'}
how do I find the most common letter in x that does not appear in say we have a string   y='aer'   so say the highest occurring letter in x is a we ignore 'a' because it appears in y and find the second highest occurring number in x say its 'e' we ignore 'e' because it appears in y and we find the next one we keep doing this until the highest occurring letter in x does not appear in y and we use that as the highest occurring letter
0 个评论
回答(2 个)
per isakson
2015-11-13
Try
x = {'overcomplete';'phellandrene';'phenanthrene';'pneumatocele';'pneumonocele'
'preintercede';'preinterfere';'pseudocumene';'pseudosphere';'spermatocele' };
y = 'aer';
xpr = regexprep( y, '(\w{1})(?=\w)', '$1\|' ); % remove the letters of y
cac = regexprep( x, xpr, '' );
str = cat( 2, cac{:} ); % concatenate all words
asc = double( str ); % convert to ascii-numbers
edges = [min(asc):1:max(asc)];
[ n, bin ] = histc( asc, edges );
ixm = find( n == max(n) );
fprintf(['highest occurring letters in x, which doesn''t appear in y, are ' ...
, '"%s" with %i occurrences\n'], char(edges(ixm)), n(ixm(1)) )
outputs
highest occurring letters in x, which doesn't
appear in y, are "np" with 11 occurrences
0 个评论
Stephen23
2015-11-13
This is very simple with mode:
>> v = +[x{:}];
>> char(mode(v(~any(bsxfun(@eq,v,y(:))))))
ans = n
Note: if there are multiple characters that occur the same number of times, then mode returns the one with the lowest character value.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!