Lua Torch张量等效函数到matlab';s";找到“;?

Lua Torch张量等效函数到matlab';s";找到“;?,lua,torch,Lua,Torch,简而言之,我想知道torch中是否有一个张量命令,它给出了张量中满足某个标准的元素的指数 下面是matlab代码,它说明了我希望能够在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 " >

简而言之,我想知道torch中是否有一个张量命令,它给出了张量中满足某个标准的元素的指数

下面是matlab代码,它说明了我希望能够在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 
我知道我可以在torch中使用for循环来实现这一点,但是否有一些等效于matlab的find命令可以让我更简洁地实现这一点

x[x:gt(5)] = 0
通常有x:gt:lt:ge:le:eq


还有一个通用的:apply函数,它接受一个匿名函数并将其应用于每个元素。

感谢您的回复!然而,我感兴趣的是如何使用索引索引到我的数组中(这是您提供的),以及如何查找索引本身(除了索引到原始张量之外,这对于其他事情通常很有用。查找索引本身可以通过一个非常难看的hack:index=torch.linspace(1,x:nElement(),x:nElement())[x:gt(5)]布尔索引似乎不适用于仅切片一个张量维度。因此,为find()/np.where()创建对应的索引将非常有用。另外,值得指出的是,
torch.linspace(1,x:nElement(),x:nElement())
并不总是提供1到x:nElement()的数字我刚刚通过艰苦的学习了解到了这一点。相反,
torch.range(1,x:neelement())
是更好的方法。