c++ - 在 xtensor 中使用 xt::where 时遇到问题

标签 c++ numpy xtensor

我试图在 xarray 中查找某些数组值的索引值。我有一个名为 latticefilled 的 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/

相关文章:

C++ - 恢复模板或转换为模板

python - 曲率的数值计算

c++ - 如何使用指针用数据填充 xtensor 数组

c++ - xtensor - Tensor 包装器在运行时引发 trivial_assigner 错误

c++ - 导致除法溢出错误 (x86)

c++ - 错误 : non-floating-point argument in call to function ‘__builtin_isnan’

c++ - 移动 Sprite 和处理事件

python-2.7 - 不同版本的 Python 2.7 和 Numpy 对同一脚本产生不同的结果

python - 如何矢量化 3D Numpy 数组