Filter löschen
Filter löschen

finding most common letter in a cell array

1 Ansicht (letzte 30 Tage)
Max
Max am 12 Nov. 2015
Beantwortet: Stephen23 am 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

Antworten (2)

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

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by