Code covered by the BSD License  

Highlights from
nonduplicate

4.0

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

nonduplicate

by

 

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

| Watch this File

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)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (5)
20 Mar 2012 Venkat R

very nice.IT works for me

04 Aug 2010 Warwick

works fine for me.
thanks.

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.

25 Jan 2010 Anon

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

25 Jan 2010 Orlando Rodríguez

What's the difference with unique?

Contact us