【问题标题】:mask and apply numpy where function to a vector in c++屏蔽并将numpy where函数应用于c ++中的向量
【发布时间】:2021-07-20 23:41:17
【问题描述】:

我正在使用 C++,并希望将向量的掩码和搜索验证条件的索引结合起来,类似于 numpy where 函数。

这是一个例子:

std::vector<int> id     = {61, 66, 68, 80}
std::vector<int> val    = {0, 0, 2, 1};
std::vector<int> offset = {62, 65, 70};
std::vector<int> masked;

使用后

masked = offset[val];

masked 应如下所示:masked = {62, 62, 70, 65}

之后,我想找到id 向量元素大于masked 的索引。两个向量具有相同的长度。这相当于 Python 中 Numpy 的 where 函数。

c = np.where(id > masked)[0]

c 等效于 vector&lt;int&gt;,应如下所示:c = {1,3}

有什么想法吗?谢谢!

【问题讨论】:

    标签: python c++ numpy where-clause mask


    【解决方案1】:

    您可以只使用一个简单的循环。请注意,您不需要中间数组masked。这是一个(未经测试的)示例:

    std::vector<int> c;
    c.reserve(val.size());
    
    for(size_t i=0 ; i<val.size() ; ++i)
        if(id[i] > Offset[val[i]])
            c.push_back(i);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 2018-07-17
      相关资源
      最近更新 更多