フィルターのクリア

How do I get the least used character in the text file ?

1 回表示 (過去 30 日間)
Tony
Tony 2015 年 4 月 25 日
コメント済み: Jan 2015 年 4 月 26 日
a= textread('GreatExpectations.txt','%c');
[m]=length(a);
most_used_letter=char(mode(0+a))
  1 件のコメント
Jan
Jan 2015 年 4 月 26 日
Is in 'aba' the 'b' or the 'c' the least used character?

サインインしてコメントする。

採用された回答

Mohammad Abouali
Mohammad Abouali 2015 年 4 月 25 日
編集済み: Mohammad Abouali 2015 年 4 月 26 日
%Sample text; equivalent to your a
txt='this is a sample text.';
uniqueChars=unique(txt(isletter(txt)));
charCount=arrayfun(@(c) sum(txt==c), uniqueChars);
% Now printing results
fprintf('Char count\n');
fprintf(' %c %d\n',[uniqueChars;charCount])
fprintf('\nThe least Used Characters are:\n')
fprintf('%c\n',uniqueChars(charCount==min(charCount)))
fprintf('\nThe most Used Characters are:\n')
fprintf('%c\n',uniqueChars(charCount==max(charCount)))
Once you run it you well get this:
Char count
a 2
e 2
h 1
i 2
l 1
m 1
p 1
s 3
t 3
x 1
The least Used Characters are:
h
l
m
p
x
The most Used Characters are:
s
t
  2 件のコメント
Tony
Tony 2015 年 4 月 26 日
Allah Kheleek , thank you!!
Mohammad Abouali
Mohammad Abouali 2015 年 4 月 26 日
編集済み: Mohammad Abouali 2015 年 4 月 26 日
You are welcome.
In larger texts, when you are sure that all characters are used you can remove uniqueChars=unique(txt(isletter(txt))); and then modify the next line form
charCount=arrayfun(@(c) sum(txt==c), uniqueChars);
to
charCount=arrayfun(@(c) sum(txt==c), char([65:90 97:122]));
You have to be sure that all characters are used though. Otherwise, those characters that are not used would be returned as the least used characters.
Also if upper/lower case are not important; then once you are done reading the text file do this:
txt=lower(txt);
and in this case if you decided to use the second form you need char(97:122) instead of char([65:90 97:122]);
If this has answered your question, I appreciate it if you accept the answers.

サインインしてコメントする。

その他の回答 (1 件)

per isakson
per isakson 2015 年 4 月 26 日
編集済み: per isakson 2015 年 4 月 26 日
Here is a code based on histc
ffs = 'GreatExpectations.txt';
fid = fopen( ffs );
buf = fscanf( fid, '%1c' );
fclose( fid );
letters only
ascii = double( upper( buf ) );
ascii( ascii<double('A') | ascii>double('Z') ) = [];
ascii_ranges = ( double('A') : double('Z') );
[ ascii_counts, ~ ] = histc( ascii, ascii_ranges );
bar( ascii_ranges, ascii_counts, 'histc' )
min_val = min( ascii_counts );
ix_min = find( ascii_counts == min_val );
fprintf( 'The letters, %s, each appears %d time(s)\n' ...
, char('A'+ix_min-1), min_val )
it prints
The letters, JQZ, each appears 1 time(s)
&nbsp
Regarding char('A'+ix_min-1) see 'ABC'-'A' Considered Harmful

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by