【问题标题】:Problem with xtensor xt::where index related functionxtensor xt::where 索引相关函数的问题
【发布时间】:2021-01-27 17:40:15
【问题描述】:

这里我尝试用 C++ 中的xtensor 库做一个非常基本的操作。我有 xarray aindex related function xt::where,我想获得条件为 True 的索引数组(注意,还有另一个 xt::where 函数,但它是 operator function我不想要它)。 当我尝试编译它时,使用这一行,我得到了很多错误:

g++ -I/usr/include/xtensor -I/usr/local/include/xtl getindx.cpp -o getindx

奇怪的是,当我尝试使用另一个 xt::where 函数(运算符函数)时,它可以工作并编译和运行。我显然错过了一些东西;我正在搜索,但我无法通过,请帮助我!谢谢。

代码如下:

#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"
#include "xtensor/xview.hpp"
#include "xtensor/xoperation.hpp"
#include "xtensor/xtensor.hpp"


using namespace std;

int main(int argc, char** argv){

  xt::xarray<double> arr {5.0, 6.0, 7.0};
  auto idx = xt::where(arr >= 6);

  std::cout << idx << std::endl;

 return 0;

}

编辑:错误。

error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::vector<std::vector<long unsigned int>, std::allocator<std::vector<long unsigned int> > >’)
   std::cout << idx << std::endl;

EDIT2:在没有 xtensor 的情况下解决。可能会慢一些。



int main(int argc, char** argv){

 
  std::vector<double> arr{5.0,6.0,7.0};
  std::vector<unsigned int> indices;
  
  auto ptr = &bits[0];
  for (int i = 0; i<arr.size(); i++, ptr++)
    {
      if (*ptr>=6) indices.push_back (i);    
   }

  for (int i=0; i<indices.size(); i++){
    cout << "indices= "indices[i] << endl;
  } //output: indices=1, indices=2.
 return 0;
}

【问题讨论】:

  • 您在auto idx = xt::where(arr &gt;= 6)); 中有一个额外的)
  • 那么错误在哪里?
  • 是的,“)”是我复制代码时的一个错误。我编辑帖子并添加错误!

标签: c++ indexing conditional-statements where-clause xtensor


【解决方案1】:

问题在于xt::where(或者语法xt::argwhere在这里更具描述性)返回一个std::vector或没有operator&lt;&lt;重载的数组索引,例如用于打印。

为了处理这个问题,xt::from_indices 创建了。来自the relevant docs page

int main()
{
    xt::xarray<size_t> a = xt::arange<size_t>(3 * 4);

    a.reshape({3,4});

    auto idx = xt::from_indices(xt::argwhere(a >= 6));

    std::cout << idx << std::endl;
}

在这种情况下,idx 也可以输入 xt::xarray&lt;size_t&gt;xt::xtensor&lt;size_t, 2&gt;,如果您希望使用 auto 更详细。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 2018-10-31
    相关资源
    最近更新 更多