Word counting in a loop

2 次查看(过去 30 天)
I have a while loop running where I bring in a sentence on every loop
Sentence(i) = ('some string of words');
I can split the words and count using the unique() function but...
I want to keep adding to a table that contains each unique word and the number of times it repeats
so it looks like...
while
Sentence(i) = ('some string of words that changes each iteration');
Words = splitlines(Sentence(i))
PL = (Words== "");
Words(PL) = [];
AnalysisWords = strings(0);
for i = 1:length(Words)
AnalysisWords = [AnalysisWords ; split(Words(i))];
end
AnalysisWords = lower(AnalysisWords);
[words,N,idx] = unique(AnalysisWords);
end
Im not sure how to turn this into a running list
  1 个评论
KSSV
KSSV 2021-2-9
Use unique and histcI to count the number of times the string repeated.

请先登录,再进行评论。

采纳的回答

Gaurav Garg
Gaurav Garg 2021-2-12
Hi,
You can look at the answer here where unique and hist functions have been used.
Rather than using hist function, you can use a map container, where words are stored as keys and thei number of occurences is stored as values.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by