Torch tensor equivalent function to matlab's “

2020-07-16 02:47发布

问题:

In a nutshell, I would like to know if there is a tensor command in torch that gives me the indices of elements in a tensor that satisfy a certain criteria.

Here is matlab code that illustrates what I would like to be able to do in torch:

my_mat = magic(3); % returns a 3 by 3 matrix with the numbers 1 through 9
greater_than_fives = find(my_mat > 5); % find indices of all values greater than 5, the " > 5" is a logical elementwise operator that returns a matrix of all 0's and 1's and finally the "find" command picks out the indices with a "1" in them
my_mat(greater_than_fives) = 0;  % set all values greater than 5 equal to 0 

I understand that I could do this in torch using a for loop, but is there some equivalent to matlab's find command that would allow me to do this more compactly?

回答1:

x[x:gt(5)] = 0

In general there are x:gt :lt :ge :le :eq

There is also the general :apply function tha takes in an anonymous function and applies it to each element.



标签: lua torch