Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Torch tensor equivalent function to matlab's "find"?

Tags:

torch

lua

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?

like image 943
elpiloto Avatar asked May 17 '15 14:05

elpiloto


Video Answer


1 Answers

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.

like image 157
smhx Avatar answered Sep 24 '22 00:09

smhx