Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

code with the same function

조회 수: 3 (최근 30일)
Gabriel Cunha
Gabriel Cunha 2019년 4월 3일
마감: MATLAB Answer Bot 2021년 8월 20일
The code below gives me the right amount of how many times a letter repeats itself in a large text.txt.
I wanted another simple code, but that would do the same thing as this, in case it gave me the number of letters in a text (A = number of letters a, B = number of letters b and so on.)
if there is no simpler than this, accept another more complicated or the same level of difficulty.
fileread('mytextfile.txt')
data = fileread('mytextfile.txt');
nnz(data=='A')
nnz(ismember(data,'A'))

답변 (2개)

Walter Roberson
Walter Roberson 2019년 4월 3일
[A, ~, AA] = unique(data);
fprintf('%c = %d\n', [A, accumarray(AA, 1)].')
  댓글 수: 8
Rik
Rik 2019년 4월 4일
Despite of its name, char_list is already a double. I didn't notice your last edit with 0+A(:), so that is why that method is capped (as chars are capped to 16 bit).
Walter Roberson
Walter Roberson 2019년 4월 4일
I did the 0+ after you (correctly) mentioned about the 65535.

Rik
Rik 2019년 4월 3일
편집: Rik 2019년 4월 4일
There are two easy options: a loop and a histogram:
%for loop method:
data = fileread('mytextfile.txt');
letters='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
counts=zeros(1,numel(letters));
for n=1:numel(letters)
counts(n)=nnz(data==letters(n));
end
%histogram method:
data = fileread('mytextfile.txt');
counts=histc(data,65:(65+25));
  댓글 수: 4
Gabriel Cunha
Gabriel Cunha 2019년 4월 4일
Your code is really incredible, but I also wanted something as simple as the code of my question that counted one letter at a time, but I will certainly study your code as well as the others who answered in order to learn more about MATLAB
Rik
Rik 2019년 4월 4일
The edited for-loop method should be a bit easier to understand.

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by