View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

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




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

| Watch this File

File Information

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).
x = nonduplicate(x);
yi = interp1(nonduplicate(x),y,xi)

returns an error.

works just fine.

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (6)
26 Feb 2016 deepa r

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

very nice.IT works for me

04 Aug 2010 Warwick

works fine for me.

25 Jan 2010 Nathan Tomlin

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 =


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 =


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.

Comment only
25 Jan 2010 Orlando Rodríguez

What's the difference with unique?

Comment only

Contact us