In Python one can do the following to get the unique values in a vector/matrix/tensor:
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:
A = [9 2 9 5];
C = unique(A)
%Now C = [9, 2, 9]
Is there an equivalent function in Torch/Lua as well?
Nope, there is no such a standard function in stock Lua and/or 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.
Example 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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.