5

我是 xtensor 的新手。我想知道如何使用 xt::where 的输出。在 python 中,例如假设 imap 是一个 nd 数组, np.where(imap>=4) 返回两个带索引的数组,可以使用 = 运算符直接分配。请让我知道如何在 xtensor C++ 上下文中使用它。任何小例子都会有很大帮助。

谢谢。

4

1 回答 1

5

返回输入类型的 xt::xarray。

xt::xarray<int> res = xt::where(b, a1, a2);

b为真则a1返回数组的元素,如果b为假则返回数组的元素a2

下面的示例是从文档中复制的(搜索xt::wherehttp://xtensor.readthedocs.io/en/latest/operator.html

b 的第一个元素是false- 所以从a2-- 11得到它

b 的第二个元素是true- 所以从a1-- 2

b 的第三个元素是true - 所以从a1-- 3

b 的第四个元素是false- 所以从a2-- 14得到它

xt::xarray<bool> b = { false, true, true, false };
xt::xarray<int> a1 = { 1,   2,  3,  4 };
xt::xarray<int> a2 = { 11, 12, 13, 14 };

xt::xarray<int> res = xt::where(b, a1, a2);
// => res = { 11, 2, 3, 14 }
于 2018-06-11T21:30:56.687 回答