计算单元格数组中的单词数 [英] count words in cell array matlab
本文介绍了计算单元格数组中的单词数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个500x1的单元格,每一行都有一个特定的单词.如何计算单词的出现次数并显示出来,并显示每次出现的百分比.
I have a 500x1 cell aray and each row has a certain word in it. How can I count how many occurences of words there is and display it and also display the percentage of each occurence.
例如
这些词的出现是:
Ans =
200 Green
200 Red
100 Blue
这些单词所占的百分比:
The percentage of these words:
Ans =
40% Green
40% Red
20% Blue
推荐答案
想法是strcmpi逐元素比较单元矩阵.这可用于将输入名称与输入中的唯一名称进行比较.尝试下面的代码.
The idea is that strcmpi compares cell matrices elementwise. This can be used to compare the input names to the unique names in the input. Try the code below.
% generate some input
input={'green','red','green','red','blue'}';
% find the unique elements in the input
uniqueNames=unique(input)';
% use string comparison ignoring the case
occurrences=strcmpi(input(:,ones(1,length(uniqueNames))),uniqueNames(ones(length(input),1),:));
% count the occurences
counts=sum(occurrences,1);
%pretty printing
for i=1:length(counts)
disp([uniqueNames{i} ': ' num2str(counts(i))])
end
我将百分比计算留给您.
I leave the percentage calculation to you.
这篇关于计算单元格数组中的单词数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文