'Interp1 error. Value X should be distinct.'
5 views (last 30 days)
Show older comments
I have matrix A below. When I try using interp1, I get this error: ??? Error using ==> interp1 at 259. The values of X should be distinct.
A= [17.4000,0.9949;
16.7000,0.9964;
15.9000,0.9978;
15.3000,0.9993;
14.5000,1.0000;
13.8000,1.0000;
13.1000,0.9985;
12.4000,0.9971;
11.7000,0.9942]
Val=interp1(A(:,1),A(:,2),15.0000)
Matlab treats 15.3 and 14.5 as 15? How do I get around this problem?
0 Comments
Answers (2)
Jan
on 15 Jan 2013
Finding duplicates is much cheaper, when you sort the data. Therefore I suggest to sort them explicitly, when you want to avoid the implicit sorting in unique():
[A1, index] = sort(A(:,1));
A2 = A(index, 2);
uniq = [true, diff(A1) ~= 0];
Value = interp1(A1(uniq), A2(uniq), 15.0000);
Btw. for a single value interp1 is not efficient. A hard-coded linear interpolation would be much cheaper. But this matters only, if this code section takes a significant part of the computing time.
Walter Roberson
on 15 Jan 2013
MATLAB R2012a outputs a value for me when I copy and paste your code.
However, if you were to alter your code slightly to
Val=interp1(A(:,2),A(:,1),15.0000)
then you would have a problem because A(:,2) contains two copies of 1.0000
Please recheck on your system.
2 Comments
Andrei Bobrov
on 15 Jan 2013
Edited: Andrei Bobrov
on 15 Jan 2013
v = [2 5 6 5 8 3];
[a,b] = unique(v,'first');
ii = sort(b);
out = a(ii);
or
for R2012a and later
out = unique(v,'stable');
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!