我试图在 xarray 中查找某些数组值的索引值。我有一个名为 lattice
filled 的 xarray,其中包含数字 1 到 n,我想要的是类似的东西
auto x2 = xt::where(lattice == i)
获取lattice
中将用于距离函数的元素i
的索引值,但我收到消息==
与操作数不匹配。当我使用 >
时,问题不会发生,所以我只是想知道有什么区别。
我在Python中使用了np.where(lattice==i)
,我正在尝试翻译它。
最佳答案
您必须使用 xt::equal(a, b)
而不是 a == b
。事实上,这与 a > b
不同,后者与 xt::greater(a, b)
完全相同。
另请注意,索引列表可以使用 xt::from_indices(...)
转换为矩阵,请参阅 documentation 。考虑以下示例:
#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;
}
关于c++ - 在 xtensor 中使用 xt::where 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58418220/