'Interp1 error. Value X should be distinct.'

5 visualizaciones (últimos 30 días)
Yun Inn
Yun Inn el 15 de En. de 2013
Editada: zohar el 4 de Jun. de 2015
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?

Respuestas (2)

Jan
Jan el 15 de En. de 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.
  1 comentario
zohar
zohar el 4 de Jun. de 2015
Editada: zohar el 4 de Jun. de 2015
+1 Just....
uniq = [true; diff(A1) ~= 0];

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 15 de En. de 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 comentarios
Yun Inn
Yun Inn el 15 de En. de 2013
Thank you. I found a duplicate somewhere else in the matrix and eliminated it with UNIQUE. However, UNIQUE sorted the data. Is there any way to eliminate duplicate without sorting?
Andrei Bobrov
Andrei Bobrov el 15 de En. de 2013
Editada: Andrei Bobrov el 15 de En. de 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');

Iniciar sesión para comentar.

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by