Return Unique Element with a Tolerance

后端 未结 7 1330
无人共我
无人共我 2020-11-27 06:40

In Matlab, there is this unique command that returns thew unique rows in an array. This is a very handy command.

But the problem is that I can\'t assign tolerance to

7条回答
  •  情深已故
    2020-11-27 06:52

    As of R2015a, there is finally a function to do this, uniquetol (before R2015a, see my other answer):

    uniquetol Set unique within a tolerance.

        uniquetol is similar to unique. Whereas unique performs exact comparisons, uniquetol performs comparisons using a tolerance.

    The syntax is straightforward:

    C = uniquetol(A,TOL) returns the unique values in A using tolerance TOL.

    As are the semantics:

    Each value of C is within tolerance of one value of A, but no two elements in C are within tolerance of each other. C is sorted in ascending order. Two values u and v are within tolerance if:
        abs(u-v) <= TOL*max(A(:),[],1)

    It can also operate "ByRows", and the tolerance can be scaled by an input "DataScale" rather than by the maximum value in the input data.

    But there is an important note about uniqueness of the solutions:

    There can be multiple valid C outputs that satisfy the condition, "no two elements in C are within tolerance of each other." For example, swapping columns in A can result in a different solution being returned, because the input is sorted lexicographically by the columns. Another result is that uniquetol(-A,TOL) may not give the same results as -uniquetol(A,TOL).

    There is also a new function ismembertol is related to ismember in the same way as above.

提交回复
热议问题