【问题标题】:matlab, each bar in histogram correspond to which letter [duplicate]matlab,直方图中的每个条对应于哪个字母[重复]
【发布时间】:2015-06-12 20:54:54
【问题描述】:

我有 400 个文件,每个文件包含大约 500000 个字符,而这 500000 个字符仅包含大约 20 个字母。我想制作一个直方图,指示使用的最多 10 个字母(x 轴)和每个字母的使用次数(y 轴)。我写了这段代码,其中缺少的是我想知道每个条对应于哪个字母。我应该在代码上添加什么?您可以更改整个代码,但保留它对我来说更好。提供我完整的代码,以便我可以直接将其复制到脚本并运行它。

     i = 1;
     z = zeros(1, 10);
        for i=1:400
    j = num2str(i);
    file_name = strcat('part',j,'txt');
    file_id = fopen(file_name);
    part = fread(file_id, inf, 'uchar');
    h = hist(part,10);
    z = z + h;
    fclose(file_id);
end

【问题讨论】:

    标签: matlab histogram


    【解决方案1】:

    首先,您对hist 的使用是错误的。 hist(data,10) 将根据包含 10 个 bin 的数据创建直方图,因此一个 bin 将对应于文件中的多个字符。

    解决此问题的一种方法是在预定义的 bin 上使用 hist,例如:

    bins = 1:255; % define the bins for hist
    histSum = zeros(numel(bins),1);
    
    for file=1:10;
        data = randi(25,100) + 'a';     %Generate random data - letters between 'a' and 'z'
        data = reshape(T,numel(T),1);   % Make it a vector
    
        histSum = histSum + hist(data,bins)';
    end
    

    请注意,您必须定义您的 bin 以容纳 所有 可能的值,因此范围从 1 到 255

    【讨论】:

      猜你喜欢
      • 2022-01-01
      • 1970-01-01
      • 2017-08-28
      • 2021-08-23
      • 1970-01-01
      • 2017-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多