My interpolation works for one set, but does not work for the other

7 visualizaciones (últimos 30 días)
Hi,
I am using the following code for interpolation from data in files. The problem is that the code runs very nicely in the first set, X1, Y1, while it shows that the X and V values don't have the same length in the second set: X2, Y2. I can't figure out the problem. Both codes present the same type of results and have same column length. The files of both sets are attached herewith.
clear; clc;
y=load ('Y1.txt');
x=load ('X1.txt');
plot(x(:,2),-sum(y(:,2:11),2))
y1=-sum(y(:,2:11),2);
x1=x(:,2);
y3=unique(y1);
x3=unique(x1);
y2 = [1308.9, 2514.9, 4797.9]*1000;
x2 = interp1(y3, x3, y2, 'linear');
figure(1)
plot(x1, y1, '-g')
hold on
plot(x2, y2, 'bp')
hold off
grid
legend('Data', 'Interpolated Points', 'Location', 'NW')
  1 comentario
Ismail Qeshta
Ismail Qeshta el 12 de Nov. de 2017
Editada: Ismail Qeshta el 12 de Nov. de 2017
Error message is the following:
Error using interp1>reshapeAndSortXandV (line 423)
X and V must be of the same length.
Error in interp1 (line 92)
[X,V,orig_size_v] = reshapeAndSortXandV(varargin{1},varargin{2});
Error in Interpolate (line 15)
x2 = interp1(y3, x3, y2, 'linear');

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 12 de Nov. de 2017
Editada: Jan el 12 de Nov. de 2017
The message is clear:
X and V must be of the same length.
Set a breakpoint in the failing line and check the dimensions:
size(y3)
size(x3)
Or:
y = load('Y2.txt');
x = load('X2.txt');
y1 = -sum(y(:,2:11),2);
x1 = x(:,2);
y3 = unique(y1);
x3 = unique(x1);
size(y3)
size(x3)
ans =
5504 1
ans =
5501 1
Then an interpolation is not possible. A solution might be:
[x3, ix] = unique(x1);
y3 = y1(ix);
  2 comentarios
Ismail Qeshta
Ismail Qeshta el 12 de Nov. de 2017
Hi Jan,
Yes. You are right. I have just checked it. Do you have any idea on how to solve this issue? Thanks.
a= size(y3)
b= size(x3)
a =
1220 1
b =
1215 1
Ismail Qeshta
Ismail Qeshta el 12 de Nov. de 2017
Thank you very much Jan. Your solution worked.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Interpolation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by