问题
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