c ++查找值的bin位置(直方图)

时间:2015-10-24 20:26:46

标签: c++ histogram bins

我是c ++的新手,所以我提前道歉,以便进行简单的编程。我有一个概率向量和另一个xvalues。我需要做的是积累概率,以便有一个增加的直方图,然后生成一个随机的统一数字,并找到它在xvales中的位置(matlab的histc函数中的ind - [bincounts,ind] = histc( ___) - )。我最终需要为随机数矢量做这个,但我想只用一个数字加注。这就是我到目前为止所做的:

int main() {
double nale=0;
nale = rand_uniforme_1(0,1); // function that creates a uniform random nunber (it works already)

double vx[] = {2,4,6,8};
std::vector<double> valorx(vx, vx+4);

double vp[]={0.2,0.3,0.1,0.4};
std::vector<double> probab(vp, vp+4);

// acumulate
std::vector<double> proacum1(probab.size());
std::partial_sum (probab.begin(), probab.end(), proacum1);

// add a 0 at the beggining
std::vector<double> proacum(probab.size()+1);
proacum[0]=0;
for (int i = 0; i < proacum1.size(); ++i) {
    proacum[i+1]=proacum1[i];
}

// position of the uniform random number
std::vector<double>::iterator low;
low=std::lower_bound (proacum.begin(), proacum.end(), nale);
int pos=(low- proacum.begin());


double value=valorx[pos];

std::cout << nale << "\n";
std::cout << value << "\n";

}

它给了我错误:

-error: no viable overloaded '=' in low=std::lower_bound (proacum.begin(), proacum.end(), nale);
-n instantiation of function template
      specialization 'std::__1::partial_sum<std::__1::__wrap_iter<double *>,
      std::__1::vector<double, std::__1::allocator<double> > >' requested here
    std::partial_sum (probab.begin(), probab.end(), proacum1);

以及我甚至不理解的更多内容。

1 个答案:

答案 0 :(得分:-1)

int pos=(low- proacum.begin());

取消参考。您正在为int分配迭代器。

我无法测试这个,因为我没有所有使用过的库和函数声明,但我相信

int pos = *(low - proacum.begin)

应该工作得很好。