xtensor xt :: where与索引相关的功能出现问题

问题描述

在这里,我尝试使用C ++中的xtensor库进行非常基本的操作。我有一个xarray a,并且有了index 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,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;
}

解决方法

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

为此创建了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<size_t>xt::xtensor<size_t,2>

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...