我有一個大約9000個小寫字的文本文件。我想找出每個單詞中最後一個字母的概率(字母的頻率/字數)。以字母結尾的可能性?
這是我第一次去:
function [ prma ] = problast()
counts = zeros(1,26);
%refer to cell index here to get alphabetic number of char
s = regexp('abcdefghijklmnopqrstuvwxyz','.','match');
f = fopen('nouns.txt');
ns = textscan(f,'%s');
fclose(f);
%8960 is the length of the file
for i =1:8960
c = substr(ns(i),-1,1);
num = find(s == c);
counts(num) = num;
end
prma = counts/8960;
disp(prma);
這給了我這個錯誤:
Undefined function 'substr' for input arguments of type 'cell'.
任何想法?
可以使用for循環討論效率問題。您可以改用直方圖(請參閱Shai的解決方案)。 –