Histogram of letters of a text
10 次查看(过去 30 天)
显示 更早的评论
Hi guys,
I want to create a histogram that shows how often a letter was used in a text but I have no idea how to count the letters or how to plot the histogram so i can see each letter at the x axis.
Does anyone have an idea how i can do it?
采纳的回答
Walter Roberson
2020-5-2
Compare the current input character against the first possible letter that you want to count. If you get a match, increment the counter associated with that letter. Otherwise compare against the second possible letter, and if there is a match, increment the counter associated with that letter. And so on. Eventually move on to the next input character.
OR
Compare all of the input characters against the first letter you want to count. Set the counter associated with that letter to the number of matches you got; do the same thing for the second letter you want to count, and so on.
Hint: you can create a vector of the letters you want to count, and do the counting in a loop.
2 个评论
Walter Roberson
2020-5-2
You can use bar() with the 'grouped' option.
Use one column (important that it be column!) in Y for each bar-in-a-group that you want drawn.
更多回答(1 个)
Rik
2020-5-2
Once you have the text in a Matlab array it is stored as numbers, so you can use the normal tools.
lorem='Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
letters=unique(lorem);
letter_counts=histcounts(double(lorem),max(letters)-min(letters)+1);
letter_counts(letter_counts==0)=[];
bar(1:numel(letters),letter_counts)
set(gca,'XTick',1:numel(letters))
set(gca,'XTickLabels',num2cell(letters))
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!