Question: In Matlab, there is this [`unique` command](http://www.mathworks.com/access/helpdesk/help/techdoc/ref/unique.html) 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 it-- in double precision, we always have to compare two elements within a precision. Is there a built-in command that returns unique elements, within a certain tolerance?

Answer: 
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

```
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`](http://www.mathworks.com/matlabcentral/fileexchange/23294-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.