I have a large data base that looks like the following:

Energy position

5.639792 1.36
4.844813 1.89
4.809105 2.33
3.954150 2.69
2.924234 3.42
1.532669 4.50
0.000000 5.63


The actual data contains thousands of rows. The first column of the data is the particle energy and second column is the particle position. The goal of the program is to create an array (or vector) that will store the values of the energy in different position bins. For the example data shown above, I would create an array with dimensions 1 x 6. For example, the first bin of the array would contain the sum of all energies with positions between 1.0 and 1.9. In this case, the first bin for the data shown above would have a value of 5.639792 + 4.844813 = 10.4846 (the sum of the first two energies). Similarly, the second position bin would contain 8.8452 the sum of the third and fourth energies. To implement this program I believe I need to use a floor ceiling function to assign the data into the proper position bin of the array. I'm not sure how to approach this problem. Any suggestions would be greatly appreciated.

Create your 1x6 array initialized to 0's
Loop through the 2nd column and take the integer (nor rounded) value of the value as an index into the 1x6 array, and add the first column's value.

numpy histogram function looks close to your needs, it is designed for using with Python language though, probably written with C though and source code is open source.

http://amusecode.org/doc/examples/simple/salpeter.html

Maybe worth to hunt through C++ libraries for histogramming?

commented: We know you LOVE Python -- but all answers in C/C++ do not have to be Python oriented. -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.