Hajdu Gábor
Hajdu Gábor

Reputation: 329

Filtering multidimensional views in xtensor

I am trying to filter a 2D xtensor view with a simple condition. I found the xt::filter function, but when i use it, it only return the first column of the filtered view. I need the 2D filtered view. What is the best way to do it?

I could check the condition line by line, and get all the indexes myself, and the use xt::view to only show the needed lines, but i am hopig in a more sophisticated method using the xtensor toolset.

My current filter, which returns only one direction looks like this:

auto unfiltered = xt::view(...);
auto filtered = xt::filter(unfiltered,  xt::view(unfiltered, xt::all(), 0) > tresh);

EDIT:

It is possible i was not completly clear. I need a 2D view where i kept only those lines, where the first element of the line is greater than the treshold.

Upvotes: 2

Views: 399

Answers (1)

johan mabille
johan mabille

Reputation: 424

xt::view(unfiltered, xt::all(), 0)

is creating a view that only contains the first column of unfiltered. The following should do what you expect:

auto unfiltered = xt::view(...);
auto filtered = xt::filter(unfiltered, unfiltered > tresh);

EDIT: sorry for the misunderstanding, here is an update following OP remark:

The condition is not broadcast to the shape of the expression to filter, a workaround for now is:

auto unfiltered = xt::view(...);
auto filtered = xt::filter(unfiltered,
                           xt::broadcast(xt::view(unfiltered, xt::all(), 0, xt::newaxis()),
                                         unfiltered.shape()) > tresh);

I'll open an issue for this.

Also notice that filter returns a 1D expression (because the elements satisfying a condition may be scattered in the original expression), you need to reshape it to get a 2D expression.

Upvotes: 2

Related Questions