Return Unique Element with a Tolerance

后端 未结 7 1232
无人共我
无人共我 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:46

    I was stuck the other day with a MatLab 2010, so, no round(X,n), no _mergesimpts (At least I couldn't get it to work) so, a simple solution that works (at least for my data):

    Using rat default tolerance:

    unique(cellstr(rat(x)))
    

    Other tolerance:

    unique(cellstr(rat(x,tol)))
    
    0 讨论(0)
  • 2020-11-27 06:50

    There is no such function that I know of. One tricky aspect is that if your tolerance is, say, 1e-10, and you have a vector with values that are equally spaced at 9e-11, the first and the third entry are not the same, but the first is the same as the second, and the second is the same as the third - so how many "uniques" are there?

    One way to solve the problem is that you round your values to a desired precision, and then run unique on that. You can do that using round2 (http://www.mathworks.com/matlabcentral/fileexchange/4261-round2), or using the following simple way:

    r = rand(100,1); % some random data
    roundedData = round(r*1e6)/1e6; % round to 1e-6
    uniqueValues = unique(roundedData);
    

    You could also do it using the hist command, as long as the precision is not too high:

    r = rand(100,1); % create 100 random values between 0 and 1
    grid = 0:0.001:1; % creates a vector of uniquely spaced values 
    counts = hist(r,grid); % now you know for each element in 'grid' how many values there are
    uniqueValues = grid(counts>0); % and these are the uniques
    
    0 讨论(0)
  • 2020-11-27 06:50

    This is hard to define well, assume you have a tolerance of 1. Then what would be the outcome of [1; 2; 3; 4]?

    When you have multiple columns a definition could become even more challenging.

    However, if you are mostly worried about rounding issues, you can solve most of it by one of these two approaches:

    1. Round all numbers (considering your tolerance), and then use unique
    2. Start with the top row as your unique set, use ismemberf to determine whether each new row is unique and if so, add it to your unique set.

    The first approach has the weakness that 0.499999999 and 0.500000000 may not be seen as duplicates. Whilst the second approach has the weakness that the order of your input matters.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-27 06:54

    This is a difficult problem. I'd even claim it to be impossible to solve in general, because of what I'd call the transitivity problem. Suppose that we have three elements in a set, {A,B,C}. I'll define a simple function isSimilarTo, such that isSimilarTo(A,B) will return a true result if the two inputs are within a specified tolerance of each other. (Note that everything I will say here is meaningful in one dimension as well as in multiple dimensions.) So if two numbers are known to be "similar" to each other, then we will choose to group them together.

    So suppose we have values {A,B,C} such that isSimilarTo(A,B) is true, and that isSimilarTo(B,C) is also true. Should we decide to group all three together, even though isSimilarTo(A,C) is false?

    Worse, move to two dimensions. Start with k points equally spaced around the perimeter of a circle. Assume the tolerance is chosen such that any point is within the specified tolerance of its immediate neighbors, but not to any other point. How would you choose to resolve which points are "unique" in the setting?

    I'll claim that this problem of intransitivity makes the grouping problem not possible to resolve, at least not perfectly, and certainly not in any efficient manner. Perhaps one might try an approach based on a k-means style of aggregation. But this will be quite inefficient, as well, such an approach generally needs to know in advance the number of groups to look for.

    Having said that, I would still offer a compromise, something that can sometimes work within limits. The trick is found in Consolidator, as found on the Matlab Central file exchange. My approach was to effectively round the inputs to within the specified tolerance. Having done that, a combination of unique and accumarray allows the aggregation to be done efficiently, even for large sets of data in one or many dimensions.

    This is a reasonable approach when the tolerance is large enough that when multiple pieces of data belong together, they will be rounded to the same value, with occasional errors made by the rounding step.

    0 讨论(0)
  • 2020-11-27 06:54

    I've come across this problem before. The trick is to first sort the data and then use the diff function to find the difference between each item. Then compare when that difference is less then your tolerance. This is the code that I use:

    tol = 0.001
    [Y I] = sort(items(:));
    uni_mask = diff([0; Y]) > tol;
    %if you just want the unique items:
    uni_items = Y(uni_mask); %in sorted order
    uni_items = items(I(uni_mask));  % in the original order
    

    This doesn't take care of "drifting" ... so something like 0:0.00001:100 would actually return one unique value.

    If you want something that can handle "drifting" then I would use histc but you need to make some sort of rough guess as to how many items you're willing to have.

    NUM = round(numel(items) / 10); % a rough guess
    bins = linspace(min(items), max(items), NUM);
    counts = histc(items, bins);
    unit_items = bins(counts > 0);
    

    BTW: I wrote this in a text-editor away from matlab so there may be some stupid typos or off by one errors.

    Hope that helps

    0 讨论(0)
提交回复
热议问题