【问题标题】:Having trouble using xt::where in xtensor在 xtensor 中使用 xt::where 时遇到问题
【发布时间】:2019-10-16 16:53:57
【问题描述】:

我正在尝试在 xarray 中查找某些数组值的索引值。我有一个名为 lattice 的 xarray,其中包含数字 1 到 n,我想要的是类似

auto x2 = xt::where(lattice == i)

获取将用于距离函数的lattice 中元素i 的索引值,但我收到== 与操作数不匹配的消息。当我使用> 时,问题不会发生,所以我只是想知道有什么区别。

我在 python 中使用了np.where(lattice==i),我正在尝试翻译它。

【问题讨论】:

    标签: c++ numpy xtensor


    【解决方案1】:

    您必须使用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;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-27
      • 1970-01-01
      • 2015-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-26
      • 1970-01-01
      • 2017-09-24
      相关资源
      最近更新 更多