在Python中,可以执行以下操作以在向量/矩阵/张量中获取唯一值:

import numpy as np

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


MATLAB中也有类似的功能:

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


Torch / Lua中也有等效功能吗?

最佳答案

不,在Lua和/或Torch中没有这样的标准功能。

考虑使用set数据结构的某些实现,滚动自己的unique()实现或重新设计您的应用程序,使其不需要这种功能。

示例11班轮:

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


相关问题:


Lua: Smartest way to add to table only if not already in table, or remove duplicates
Lua : remove duplicate elements

关于python - 与MATLAB或Numpy'Unique'等效的Torch/Lua函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36458573/

10-12 23:23