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

```
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)))

```