5

I am new to the xtensor. I am wondering how one would use the output from xt::where. In python, for example assuming imap is an nd array, np.where(imap>=4) returns two arrays with indices and can be assigned directly using the = operator. Please let me know how to use it in the xtensor C++ context. Any small example would be a great help.

Thanks.

Achyut Sarma
  • 119
  • 7
  • What you're describing is really `np.nonzero(imap >= 4)`, which `np.where(one_arg)` is an unfortunate shorthand for. – Eric Jun 12 '18 at 00:20

1 Answers1

5

returns xt::xarray of input type.

xt::xarray<int> res = xt::where(b, a1, a2);

b is a true then elements of array a1 are returned, if b is false elements of a2 are returned.

The below example is copied from documentation (search for xt::where) http://xtensor.readthedocs.io/en/latest/operator.html

b's first element is false - so get it from a2 -- 11

b's second element is true - so get it from a1 -- 2

b's third element is true - so get it from a1 -- 3

b's fourth element is false - so get it from a2 -- 14

xt::xarray<bool> b = { false, true, true, false };
xt::xarray<int> a1 = { 1,   2,  3,  4 };
xt::xarray<int> a2 = { 11, 12, 13, 14 };

xt::xarray<int> res = xt::where(b, a1, a2);
// => res = { 11, 2, 3, 14 }
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34