lua - torch 张量等效函数到 matlab 的 "find"?

标签 lua torch

简而言之,我想知道 Torch 中是否有一个张量命令,它为我提供了满足特定条件的张量中元素的索引。

这是 matlab 代码,说明了我希望能够在火炬中做什么:

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 命令可以让我更紧凑地执行此操作?

最佳答案

x[x:gt(5)] = 0

一般有 x:gt :lt :ge :le :eq

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

关于lua - torch 张量等效函数到 matlab 的 "find"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30288203/

相关文章:

python - 如何动态索引pytorch中的张量?

Lua - 魔兽世界 API Debuff 框架

python - 如何将 Python 脚本转换为 Lua 脚本?

lua - Lua中模式匹配的几个模式匹配问题

lua - 期望 nn.Sum 出现连续的张量错误

python - PyTorch:时间序列任务的数据加载器

https - 具有相同 header 的 firefox 和 luasocket 之间的差异

unicode - 我如何使用 io.open 在 lua 中打开 unicode 路径

ubuntu 的 QtCore 安装

lua - 使用 torch7 模型测试单个图像