Yae*_*778 1 matlab text word-frequency
我有很多单词的向量,我想要一个只有唯一单词的向量,以及每个单词的频率.我已经尝试了hist,histc但它们是数值的.我知道这个功能,tabulate但它给出了一些'(例如,这转向'这个').如果你有任何想法如何做它MATLAB它会很棒.谢谢
你走在正确的轨道上!只需unique先使用即可为数字输入做准备hist.诀窍在于,返回的单词occurence ids unique可以用作hist函数的输入,因此您可以在没有显式for循环的情况下获取计数:
words = {'abba' 'bed' 'carrot' 'damage' 'bed'};
[unique_words, ~, occurrences] = unique(words);
unique_counts = hist(occurrences, 1:max(occurrences));
Run Code Online (Sandbox Code Playgroud)
这会产生:
>> unique_words
'abba' 'bed' 'carrot' 'damage'
>> unique_counts
1 2 1 1
Run Code Online (Sandbox Code Playgroud)