Search code examples
indexingnanxtensor

Xtensor return indicies where value is NaN


Suppose I have a tensor:

xt::xtensor_fixed<float, xt::xshape<1, 4>, xt::layout_type::column_major> withnans =
 { {3, std::numeric_limits<double>::quiet_NaN(), 4, std::numeric_limits<double>::quiet_NaN()} };

i.e. {{3, nan, 4, nan}}

And I wanted to return the positions (indexes) of where there is a nan.

{1, 3}

How would I do this in xtensor?


Solution

  • Never mind.

    xt::xtensor_fixed<float, xt::xshape<1, 4>, xt::layout_type::column_major> withnans =
     { {3, std::numeric_limits<double>::quiet_NaN(), 4, std::numeric_limits<double>::quiet_NaN()} };
    
    std::cout << xt::flatten_indices(xt::argwhere(xt::isnan(xt::flatten(withnans)))) << std::endl;
    
    

    {3}