gpt4 book ai didi

numpy - xt::where 用于 xtensor C++ 的示例用法

转载 作者:行者123 更新时间:2023-12-04 06:09:38 27 4
gpt4 key购买 nike

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

谢谢。

最佳答案

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

xt::xarray<int> res = xt::where(b, a1, a2);
b 为真则返回数组 a1 的元素,如果 b 为假,则返回 a2 的元素。

以下示例是从文档中复制的(搜索 xt::where )
http://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 }

关于numpy - xt::where 用于 xtensor C++ 的示例用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50803862/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com