Word counting in a loop

조회 수: 7 (최근 30일)
Xymbu
Xymbu 2021년 2월 9일
답변: Gaurav Garg 2021년 2월 12일
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에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by