Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

example usage of xt::where for xtensor C++

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.

like image 899
Achyut Sarma Avatar asked Jun 11 '18 18:06

Achyut Sarma


1 Answers

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 }
like image 54
Pavan Chandaka Avatar answered Oct 20 '22 08:10

Pavan Chandaka