Matlab图形直方图,指示文件中每个字符的总和 [英] matlab plot histogram indicating sum of each character inside a file
问题描述
我有400个文件,每个文件包含大约500000个字符,而这500000个字符仅由大约20个字母组成.我想制作一个直方图,指示使用的最多10个字母(x轴)和每个字母的使用次数(y轴).我该怎么做.
I have 400 files, each one contains about 500000 character, and those 500000 characters consists only from about 20 letters. I want to make a histogram indicating the most 10 letters used (x-axis) and number of times each letter is used (y-axis). how can i make it.
推荐答案
由于具有uchar
数组,因此您知道元素将始终在0:255
范围内.在看到TamásSzabó的答案在这里后,我意识到文本文件中的空字符极少出现,因此我将忽略它并使用范围1:255
.如果希望使用空字符,则必须调整范围.
Since you have an array of uchar
, you know that your elements will always be in the range 0:255
. After seeing Tamás Szabó's answer here I realized that the null character is exceedingly unlikely in a text file, so I will just ignore it and use the range 1:255
. If you expect to have null characters, you'll have to adjust the range.
为了找到10个最常用的字母,我们将首先计算直方图计数,然后按降序对它们进行排序并取前10个:
In order to find the 10 most frequently-used letters, we'll first calculate the histogram counts, then sort them in descending order and take the first 10:
counts = histc(uint8(part), [1:255]);
[topCounts, topIndices] = sort(counts, 'descend');
现在,我们需要重新排列计数和索引,以按字母顺序将字母放回原位:
Now we need to rearrange the counts and indices to put the letters back in alphabetical order:
[sortedChars, shortIndices] = sort(topIndices(1:10));
sortedCounts = topCounts(shortIndices);
现在我们可以使用bar
绘制直方图了:
Now we can plot the histogram using bar
:
bar(sortedCounts);
(如果希望图形中的条像正常的hist
图中的条一样触摸,则可以添加'hist'
选项.)
(You can add the 'hist'
option if you want the bars in the graph touching like they do in the normal hist
plot.)
要将水平图例从数字值更改为字符,请使用sortedChars
作为'XtickLabel'
:
To change the horizontal legend from numeric values to characters, use sortedChars
as the 'XtickLabel'
:
labelChars = cellstr(sortedChars.').';
set(gca, 'XtickLabel', labelChars);
这篇关于Matlab图形直方图,指示文件中每个字符的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!