2

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?

Amir
  • 10,600
  • 9
  • 48
  • 75

1 Answers1

1

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:

Community
  • 1
  • 1
Kamiccolo
  • 7,758
  • 3
  • 34
  • 47
  • this does not work! at least for 99.99999% of all cases. ipairs will cause lots of problems here. – Piglet Apr 06 '16 at 18:36
  • @Piglet would You care to elaborate more on those... 99.99999% cases? – Kamiccolo Apr 06 '16 at 18:43
  • ipairs only iterates over tables with integer keys from 1 to n. it will stop a the first element that is nil. So you will possibly not take all elements of input_table into consideration. then you set unique_elements[value] true. This will only work if value is either a string or an integer and it will create a table with elements at keys that are most likely not 1...n without any gap. which of course would be needed in your second for loop where you use ipairs again to fill output_table. – Piglet Apr 06 '16 at 18:47
  • Yup, realized the last `ipairs()` call. That was a typo, fixed. Not sure if vector supposed to have non-integer keys, or keys at all. So, no gaps as well. – Kamiccolo Apr 06 '16 at 18:48
  • Every unique value. I would benchmark the performance with extra and not needed check, because that just sound like a couple of extra ticks. – Kamiccolo Apr 06 '16 at 18:53
  • @Piglet read the question again, please. Wasn't it the requirement? To quote the OP: `a = np.unique([1, 1, 2, 2, 3, 3]) # Now a = array([1, 2, 3])` And setting `unique_elements[2] = true` twice does not change the output anyhow. That's what I've said in my last comment, no need extra burden checking the existing value. That's just an extra `if` clause. – Kamiccolo Apr 06 '16 at 18:58
  • my bad. who chose this stupid function name for this purpose :D but you still have the issue that your input is very limited. – Piglet Apr 06 '16 at 18:59
  • That's an unfortunate that lua/Torch do not offer such useful functions. Thanks for your effort though. – Amir Apr 07 '16 at 00:39