1

我是初学者,xtensor我目前正在寻找从数组列表中获取行。

我有以下矩阵。

auto matrix = {{  0.,   1.,   0.,   1.,   1.,   1.,   1.},
               {  1.,   2.,   0.,   1.,   3.,   1.,   1.},
               {  2.,   3.,   0.,   2.,   7.,   0.,   0.},
               {  3.,   4.,   0.,   1.,  11.,   0.,   1.},
               {  4.,   0.,   1.,   1.,   0.,   0.,   0.}}

从这个矩阵中,我想选择以下行。

xt::xarray<int> rowIndices = { 1, 2, 3, 4 }

现在我想使用这个rowIndices数组来获得一个包含所有行的子矩阵。我怎样才能做到这一点?

我尝试了以下。

xt::view(matrix, rowIndices, xt::all())

但这不起作用。

4

1 回答 1

1

您需要用于xt::keep(...)按索引选择行。

完整的例子:

#include <xtensor/xtensor.hpp>
#include <xtensor/xview.hpp>
#include <xtensor/xio.hpp>

int main()
{
  xt::xtensor<double,2> a =
    {{  0.,   1.,   0.,   1.,   1.,   1.,   1.},
     {  1.,   2.,   0.,   1.,   3.,   1.,   1.},
     {  2.,   3.,   0.,   2.,   7.,   0.,   0.},
     {  3.,   4.,   0.,   1.,  11.,   0.,   1.},
     {  4.,   0.,   1.,   1.,   0.,   0.,   0.}};

  xt::xtensor<size_t,1> rowIndices = { 1, 2, 3, 4 };

  auto v = xt::view(a, xt::keep(rowIndices), xt::all());

  std::cout << v << std::endl;

  return 0;
}

打印:

{{  1.,   2.,   0.,   1.,   3.,   1.,   1.},
 {  2.,   3.,   0.,   2.,   7.,   0.,   0.},
 {  3.,   4.,   0.,   1.,  11.,   0.,   1.},
 {  4.,   0.,   1.,   1.,   0.,   0.,   0.}}

请注意,根据文档,在视图中您还可以使用xt::range(...), xt::all(), xt::newaxis(), xt::keep(...), 和xt::drop(...).

于 2019-11-13T09:58:18.037 回答