我有一组 GSL 直方图,用于制作一组概率分布函数,根据文档,它们存储在一个结构中,如下所示:
Data Type: gsl_histogram_pdf
size_t n
This is the number of bins used to approximate the probability distribution function.
double * range
The ranges of the bins are stored in an array of n+1 elements pointed to by range.
double * sum
The cumulative probability for the bins is stored in an array of n elements pointed to by sum.
我打算使用 KS 测试来确定数据是否相似。因此,我试图访问此结构中给定 bin 的总和,以计算“距离”,并且我认为,我应该能够通过使用以下方式访问该值:
((my_type)->pdf->sum+x)
其中 X 是 bin 编号。
然而,无论我做什么,它总是返回 0,有人知道出了什么问题吗?
提前致谢
---- 编辑 ----
这是我处理 pdf/直方图的代码片段:
/* GSL Histogram creation */
for (i = 0; i < chrom->hits; i++) {
if ( (chrom+i)->spectra->peaks != 0 ) {
(chrom+i)->hist = gsl_histogram_alloc(bins);
gsl_histogram_set_ranges_uniform((chrom+i)->hist, low_mz, high_mz);
for (j = 0; j < (chrom+i)->spectra->peaks; j++) {
gsl_histogram_increment( (chrom+i)->hist, ((chrom+i)->spectra+j)->mz_value);
}
} else {
printf("0 value encountered!\n");
}
}
/* Histogram probability distribution function creation */
for (i = 0; i < chrom->hits; i++) {
if ( (chrom+i)->spectra->peaks != 0 ) {
(chrom+i)->pdf = gsl_histogram_pdf_alloc(bins);
gsl_histogram_pdf_init( (chrom+i)->pdf, (chrom+i)->hist);
} else {
continue;
}
}
/* Kolmogorov-Smirnov */
float D;
for (i = 0; i < chrom->hits-1; i++) {
printf("%f\n",((chrom+i)->pdf->sum+25));
for (j = i+1; j < chrom->hits; j++) {
D = 0;
diff = 0;
/* Determine max distance */
}
}
最佳答案
您计算一个指向您想要访问的值的指针。
更改当前的指针计算
printf("%f\n",((chrom+i)->pdf->sum+25));
或者是普通数组下标
printf("%f\n",(chrom+i)->pdf->sum[25]);
或指针计算,然后取消引用
printf("%f\n",*((chrom+i)->pdf->sum+25));
看看这是否能解决您的问题。该值也不应该为 0,但它很可能显示为 0,因为它可能表示一个非常小的 float ,具体取决于内存虚拟布局。
关于c - C 语言的 GNU 科学库概率分布函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11826103/