【问题标题】:Locating a bin in boost histogram在提升直方图中定位一个 bin
【发布时间】:2019-08-01 15:20:05
【问题描述】:

我正在尝试新的 boost::histogram 库,但在一些基本用法上遇到了问题。将新数据点添加到直方图时,我想知道分配给它的 bin 的索引。插入的方法返回 void 所以我试图使用 STL 算法来定位 bin 但编译器抱怨。这是代码,从文档中的一个示例修改

#include <iostream>
#include <sstream>
#include <algorithm> 
#include <vector>
#include <string>

#include <boost/format.hpp>    // only needed for printing
#include <boost/histogram.hpp> // make_histogram, regular, weight, indexed

namespace bh = boost::histogram;
using namespace std;

int main() {  
  vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0};

  auto h = bh::make_static_histogram(bh::axis::variable<>(v.begin(), v.end()));

  vector<double> data = {-0.5, 1.1, 2.3, 3.7, 3.0, 2.999};
  std::for_each(data.begin(), data.end(), std::ref(h));

  auto it = lower_bound(h.begin(), h.end(), 3.0);
  cout << it.idx(0) << endl;

  return 0;
}

编译器会吐出一堆错误,但我认为归结为lower_bound 无法进行对象比较的事实。库文档似乎暗示支持 STL 算法。 感谢您的帮助

【问题讨论】:

    标签: c++ boost histogram


    【解决方案1】:

    这行得通。

        auto v_test = axis::variable<>{ 1.0, 2.0, 3.0, 4.0, 5.0 };
        auto h_test = make_histogram(v_test);
    
        vector<double> data = { -0.5, 1.1, 2.3, 3.7, 3.0, 2.999 };
        std::for_each(data.begin(), data.end(), std::ref(h_test));
    
        auto it_test = std::lower_bound(h_test.begin(), h_test.end(), 3.0);
        auto pos_test = (it_test - h_test.begin()) - 1;
    
        cout << "here is pos" << pos_test << '\n';
    

    【讨论】:

      【解决方案2】:

      您也可以直接使用轴来计算 bin。例如(针对 Boost 1.73 更新):

      #include <iostream>
      #include <algorithm> 
      #include <vector>
      #include <string>
      
      #include <boost/histogram.hpp>
      
      namespace bh = boost::histogram;
      
      int main() {  
        std::vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0};
      
        auto h = bh::make_histogram(bh::axis::variable<>(v));
      
        std::vector<double> data = {-0.5, 1.1, 2.3, 3.7, 3.0, 2.999};
        h.fill(data);
      
        auto bin = h.axis(0).index(3.0);
        std::cout << bin << std::endl;
      
        return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2016-10-14
        • 1970-01-01
        • 2019-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-07
        • 1970-01-01
        相关资源
        最近更新 更多