微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。