フィルターのクリア

How do I get the program to display the most and least Freq used words? Tells me the # times used but I dont know how to get it to display the words. PLease Help!

1 回表示 (過去 30 日間)
a =fileread('GreatExpectations.txt');
w = regexpi(a, ' ', 'split');
v = unique(w);
n = length(v);
counts = zeros(n,1);
for i=1:n
counts(i) = sum(strcmp(w, v{i}));
end
most_frequent =max(counts)
for i=1:n
counts(i) = sum(strcmp(w, v{i}));
end
least_frequent= min(counts)

採用された回答

Star Strider
Star Strider 2015 年 4 月 25 日
The easiest way to do this is with accumarray:
a = fileread('GreatExpectations.txt');
w = regexpi(a, ' ', 'split');
[v,~,ic] = unique(w);
counts = accumarray(ic, 1);
imx = find(counts == max(counts));
imn = find(counts == min(counts));
fprintf(1,'\n\tMost frequent: “%s” appears %d times\n', v{imx}, counts(imx))
fprintf(1,'\n\tThere are %d that only appear one time\n\n', length(imn))
I used find rather than the second output of max and min because they only return the first index of the occurrence of the maximum or minimun while find returns them all.
The code produces:
Most frequent: “the” appears 25 times
There are 188 that only appear one time
  2 件のコメント
Star Strider
Star Strider 2015 年 4 月 25 日
My pleasure!
If my Answer solved your problem, please Accept it.

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

その他の回答 (1 件)

Ahmet Cecen
Ahmet Cecen 2015 年 4 月 25 日
add to the end:
v{counts==most_frequent}
v{counts==least_frequent}

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by