火炬张量的等效函数与MATLAB的“查找"? [英] Torch tensor equivalent function to matlab's "find"?

查看:103
本文介绍了火炬张量的等效函数与MATLAB的“查找"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我想知道在炬管中是否存在张量命令,该命令可以为我提供满足特定条件的张量元素索引.

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.

以下是matlab代码,它说明了我希望在火炬中实现的功能:

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 

我知道我可以使用for循环在焊炬中执行此操作,但是是否存在与matlab的find命令等效的功能,可以让我更紧凑地执行此操作?

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?

推荐答案

x[x:gt(5)] = 0

通常有x:gt:lt:ge:le:eq

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

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

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

这篇关于火炬张量的等效函数与MATLAB的“查找"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆