【问题标题】:need code or logic behind hist command in matlab [closed]在matlab中需要hist命令背后的代码或逻辑[关闭]
【发布时间】:2013-06-30 03:29:19
【问题描述】:

我需要帮助。我正在使用使用 hist 命令的 matlab 代码,我需要 java 等效代码或 matlab 中 hist 函数背后的逻辑,以便我可以在 java 或 c 中对其进行编码。提前致谢。

【问题讨论】:

  • 你可以试试edit hist看看matlab实现hist的方法。
  • 有各种各样的选项——它可以表现的不同方式(返回值、创建箱、绘图、多个数据集、注释......)。如果您简化您的问题,这将很有帮助 - 例如,“如何创建一个 C 数组,其中包含与数组 X(50,1) 对应的一系列箱,我事先不知道我需要多少箱,但是垃圾箱必须是10 宽且间隔均匀”。我们也许可以提供帮助 - 但对于一般情况,您可以遵循 @natan 的建议(但我什至不确定它是否会有帮助 - 这是一个非常复杂的功能,有很多选项)。

标签: java matlab equivalent


【解决方案1】:

直方图的简单逻辑(假设您知道宽度恒定的 bin,并且您不需要尽可能高效的代码):

float x[50]; // assumed to be array of data values
float binWidth, firstBin; // bins of width binWidth; first one centered on firstBin
int numBins; // number of bins
int *bins, tooSmall = 0, tooLarge = 0, ii, indx;

bins = (int*)calloc(numBins * sizeof(int)); // allocate, set to zero

for(ii = 0; ii < 50; ii++) {
  indx = floor((x[ii]-firstBin)/binWidth + 0.5);
  if (index < 0 ) {
    tooSmall++;
    }
    elseif (index >= numBins) {
      tooLarge++;
    }
    else {
      bins[indx]++;
    }
  }
}

最后,您会看到 x 中数据的直方图,其中有两个计数器对应于超出范围(低于或超出范围)的数据。

免责声明:编写时无需编译器进行测试。看起来“大约正确” - 在依赖它之前先在已知案例上进行测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 2019-05-29
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多