Determining the number of occurrences of each word in cell array

十年热恋 提交于 2019-12-11 13:53:24

问题


I have huge vector of words, and I want a vector with the unique words only, and the frequency for each word. I've already tried hist and histc but they are for numeric value. I know the function tabulate but it gives the words some ' (e.g this turns to 'this'). If you have any idea how to do it MATLAB it would be great. thanks


回答1:


You were on the right track! Just use unique first to prepare the numeric input for hist. The trick is that the word occurence ids returned by unique can be used as input for the hist function, so you can get the counts without explicit for loops:

words = {'abba' 'bed' 'carrot' 'damage' 'bed'};
[unique_words, ~, occurrences] = unique(words);
unique_counts = hist(occurrences, 1:max(occurrences));

This yields:

>> unique_words 
    'abba'    'bed'    'carrot'    'damage'

>> unique_counts
     1     2     1     1


来源:https://stackoverflow.com/questions/27488207/determining-the-number-of-occurrences-of-each-word-in-cell-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!