finding most common letter in a cell array

조회 수: 1 (최근 30일)
Max
Max 2015년 11월 12일
답변: Stephen23 2015년 11월 13일
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