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 &nbsp y='aer' &nbsp 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

回答(2 个)

per isakson
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

Stephen23
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.

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by