0

I want to program a function in C++ which does the same thing as the function histcounts in Matlab, but I don't get the right edges.

[V, edges]=histcounts(Vector,10);

I found this post : implementing matlab hist() in c++ and I created a function which I thought should work. How do I get the same edges in my function as in histcounts?

My Code :

int n =10;
double histogrammdistance = (ceil(*max_element(std::begin(Vector), std::end(Vector)))-floor(*min_element(std::begin(Vector), std::end(Vector))))/n;
vector<double> edges;
double minY = *min_element(std::begin(Vector), std::end(Vector));

for (int i = 0 ;i <=n; i++)
{
    edges.push_back(floor(minY) +  histogrammdistance * i);
}

So My Problem right now is :

i have this Vector = [-37,0329218106996 -26,9722222222222 -34,0823045267490 -33,0987654320988 -39 -35,0658436213992 -30,8061224489796 -36,0493827160494 -38,0164609053498 -12]

Matlab creates these edges :

edges [-40 -37,2000000000000 -34,4000000000000 -31,6000000000000 -28,8000000000000 -26 -23,2000000000000 -20,4000000000000 -17,6000000000000 -14,8000000000000 -12,0000000000000]

But my Program doesn't use -40 to calculate the binwidth..because the minimum number is -39 and not -40. Also if i change -39 to -38.5... matlab is still picking -40

1, Question : Why is Matlab taking -40 ... and has sb an idea how this could be implemented ?

i created now a very simpel Vector with [1 2 3 4 5] if i take 15 as bin number i get this as solution [1 1.27 1.54 1.81 ...] bit if i take 13 as bin number it doesn*t start with the min number 1 :/ it starts with [0,90 1,2 1,54 1,8 2,18....]

2. Question : do sb know why it took 0.90 ??

  • It looks like Matlab is taking the floor of the min value. I'm not sure what it does with the max value, you'd have to give it a non-integer max and see. – beaker Aug 24 '17 at 14:32
  • @beaker Oh .. Jes you 're right .. It seems that he is using also floor for the maximum :) but now i have another Problem :S i created now a very simpel Vector with [1 2 3 4 5] if i take 15 as bin number i get this as solution [1 1.27 1.54 1.81 ...] bit if i take 13 as bin number it doesn*t start with the min number 1 :/ it starts with [0,90 1,2 1,54 1,8 2,18....] do you or sb else have an idea why it took 0.90 ?? – Rocket_boy Aug 24 '17 at 15:19
  • Can you add the updates to the question itself? Also, I would expect that Matlab would either use `ceil` for the maximum, or just leave it alone. If you use `floor` you'd miss counting a non-integer max value. – beaker Aug 24 '17 at 15:21
  • i updated the code in the question with floor and ceil ... and also changed the question .. maybe you have an idea to Qustion 1 and 2 :/ – Rocket_boy Aug 24 '17 at 18:04
  • I have no idea, but it would probably help if you put the full vectors of bin edges rather than cutting them off. – beaker Aug 24 '17 at 19:03

0 Answers0