Code covered by the BSD License

### Highlights from nonduplicate

4.0
4.0 | 2 ratings Rate this file 2 Downloads (last 30 days) File Size: 1.67 KB File ID: #26470 Version: 1.0

# nonduplicate

### Nathan Tomlin (view profile)

Makes all values of input array distinct - used to get interp1 to work when x data has duplicates.

File Information
Description

Fixes x data so that interp1 doesn't return error:
'The values of X should be distinct.'

Checks for duplicate values in input 1D data array and returns array where all values are distinct. If duplicates are found, values are separate by small value (eps).

useage:
x = nonduplicate(x);

yi = interp1(nonduplicate(x),y,xi)

Example:
interp1([1,1,2],[1,1,2],1.5)
returns an error.

interp1(nonduplicate([1,1,2]),[1,1,2],1.5)
works just fine.

MATLAB release MATLAB 7.4 (R2007a)
26 Feb 2016 deepa r

### deepa r (view profile)

This works fine except that the order of the array changes. Is there any way I can keep the order same after using nonduplicate.

Comment only
20 Mar 2012 Venkat R

### Venkat R (view profile)

very nice.IT works for me

04 Aug 2010 Warwick

### Warwick (view profile)

works fine for me.
thanks.

25 Jan 2010 Nathan Tomlin

### Nathan Tomlin (view profile)

Orlando - unique returns a smaller array, [1,1,2] becomes [1,2]. While nonduplicate keeps the same number of values, but slightly shifts them so [1,1,2] becomes [1,1+eps,2]. The idea was to get interp1 to work by changing the values as little as possible.

'Anon' - Great point, this code is not a good idea if there is a lot of noise or large outliers in the y data. I hadn't heard of consolidator - I will check it out.

Comment only
25 Jan 2010 Anon

### Anon (view profile)

You implicitly assume that the last of a subsequent number of repetitions in the x vector is the "valid" one. But do you think this assumption is always justified?

x = [1,1,1,2];
y = [1,5,1,2];
xi = 1.5;
yi = interp1(nonduplicate(x),y,1.5)

yi =

1.500000000000000

Duplicate values in x may arise from multiple measurements at the same location (in time or in space or elsewhere). As such, the above could also look like this:

x = [1,1,1,2];
y = [1,1,5,2];
xi = 1.5;
yi = interp1(nonduplicate(x),y,1.5)

yi =

3.500000000000001

In such case, wouldn't it be better to e.g. take the mean of y values for repeated values in x? There might be cases where your strategy might be a reasonable way to address this problem, but in general, I'd strongly recommend to use consolidator available on the FEX.

http://www.mathworks.com/matlabcentral/fileexchange/8354-consolidator

Comment only
25 Jan 2010 Orlando RodrÃ­guez

### Orlando RodrÃ­guez (view profile)

What's the difference with unique?

Comment only