finding most common letter in a cell array

1 view (last 30 days)
Max
Max on 12 Nov 2015
Answered: Stephen23 on 13 Nov 2015
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

Answers (2)

per isakson
per isakson on 13 Nov 2015
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 on 13 Nov 2015
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.

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!