finding unique phrases and their frequency in a cell array

1 回表示 (過去 30 日間)
Danielle Leblance
Danielle Leblance 2020 年 4 月 13 日
コメント済み: Star Strider 2020 年 4 月 14 日
Hi,
I usually deal with numbers but never with text. I have a cell array of phrases(attached the mat file) . I want to know what are these uniques phrases and the frequency of each phrase (how many time it appears). Any help would be greatly appreciated.

採用された回答

Star Strider
Star Strider 2020 年 4 月 13 日
Try this:
D = load('matlab.mat');
nbEmployees = D.nbEmployees;
[uE,~,ix] = unique(nbEmployees);
tally = accumarray(ix, 1);
Out = table(uE, tally, 'VariableNames',{'Phrase','Frequency'})
producing:
Out =
13×2 table
Phrase Frequency
______________________________________ _________
{'Between 1,000 and 2,999 employees'} 1
{'Between 100 and 149 employees' } 5
{'Between 15 and 24 employees' } 11
{'Between 150 and 199 employees' } 1
{'Between 200 and 249 employees' } 1
{'Between 25 and 49 employees' } 9
{'Between 250 and 499 employees' } 1
{'Between 5 and 14 employees' } 25
{'Between 50 and 74 employees' } 9
{'Between 500 and 749 employees' } 10
{'Between 75 and 99 employees' } 4
{'I don't know' } 3
{'Under 5 employees' } 70
.

その他の回答 (1 件)

Danielle Leblance
Danielle Leblance 2020 年 4 月 14 日
編集済み: Danielle Leblance 2020 年 4 月 14 日
It appears that the solution is problematic for frequency of observations when you have blanck cells. I received for instance the following output:
  1 件のコメント
Star Strider
Star Strider 2020 年 4 月 14 日
Your file did not have blank cells.
Experiment with this:
D = load('matlab.mat');
nbEmployees = D.nbEmployees;
nbEmployees = nbEmployees(cellfun(@(x)~isempty(x), nbEmployees),:);
[uE,~,ix] = unique(nbEmployees);
tally = accumarray(ix, 1);
Out = table(uE, tally, 'VariableNames',{'Phrase','Frequency'})
That runs without error, and assumes that ‘blank’ elements are ‘empty’ elements.
.

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by