我正在尝试在 xarray 中查找某些数组值的索引值。我有一个名为lattice
填充的 xarray,其中包含数字 1 到 n,我想要的是类似
auto x2 = xt::where(lattice == i)
获取将用于距离函数的元素的索引值,但我得到的消息i
与操作数不匹配。当我使用时,问题不会发生,所以我只是想知道有什么区别。lattice
==
>
我np.where(lattice==i)
在 python 中使用过,我正在尝试翻译它。
您必须使用xt::equal(a, b)
而不是a == b
. 实际上,这与 examplea > b
完全一样xt::greater(a, b)
。
另请注意,可以使用 将索引列表转换为矩阵xt::from_indices(...)
,请参阅文档。考虑以下示例:
#include <xtensor/xtensor.hpp>
#include <xtensor/xio.hpp>
int main()
{
xt::xtensor<size_t,2> a = xt::arange(5 * 5).reshape({5, 5});
size_t i = 4;
xt::xtensor<size_t,2> idx = xt::from_indices(xt::where(xt::equal(a, i)));
std::cout << idx << std::endl;
return 0;
}