简体   繁体   English

Torch/Lua 等效于 MATLAB 或 Numpy 'Unique' 的函数

[英]Torch/Lua equivalent function to MATLAB or Numpy 'Unique'

In Python one can do the following to get the unique values in a vector/matrix/tensor:在 Python 中,可以执行以下操作以获取向量/矩阵/张量中的唯一值:

import numpy as np

a = np.unique([1, 1, 2, 2, 3, 3])
# Now a = array([1, 2, 3])

There is a similar function in MATLAB as well: MATLAB 中也有类似的函数:

A = [9 2 9 5];
C = unique(A)
%Now C = [9, 2, 9]

Is there an equivalent function in Torch/Lua as well? Torch/Lua 中是否也有等效的功能?

Nope, there is no such a standard function in stock Lua and/or Torch.不,Lua 和/或 Torch 中没有这样的标准功能。

Considering using some implementation of set data structure, rolling Your own implementation of unique() or redesigning Your application not to require this kind of functionality.考虑使用set数据结构的一些实现,滚动您自己的unique()或重新设计您的应用程序以不需要这种功能。

Example 11-liner:示例 11-liner:

function vector_unique(input_table)
    local unique_elements = {} --tracking down all unique elements
    local output_table = {} --result table/vector

    for _, value in ipairs(input_table) do
        unique_elements[value] = true
    end

    for key, _ in pairs(unique_elements) do
        table.insert(output_table, key)
    end

    return output_table
end

Related questions:相关问题:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM