How to resolve this error message in interpolate1?
Show older comments
Hi, I am using the following code for interpolation:
clear; clc;
y=load ('xx11.0.txt');
x=load ('yy11.0.txt');
plot(x(:,2),-sum(y(:,2:11),2))
xlabel('X')
ylabel('Y')
y1=-sum(y(:,2:11),2);
x1=x(:,2);
y3=unique(y1);
x3=unique(x1);
y2 = [9080.954]*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')
But I keep getting the following error message:
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');
11 Comments
Paolo
on 27 May 2018
Ismail,
The error is telling you that the dimensions of y3 and x3 are different. I ran the code and found that y3 is 5503x1, whereas x3 is 5501x1.
Ismail Qeshta
on 27 May 2018
For testing purposes I removed the last two points from y3 as follows:
y3= y3(1:end-2);
The code ran successfully. The graphical output:

Ismail Qeshta
on 27 May 2018
dpb
on 27 May 2018
Because you used
y3=unique(y1);
x3=unique(x1);
to get the x,y values for the interpolant and it turns out there are different numbers that are unique for the two.
...
x2 = interp1(y3, x3, y2, 'linear');
By doing that independently, you also may have rearranged the x,y relationship somewhat in the area(s) in which there are duplicated values.
You might want to use the 'rows' option to make both x and y disappear where they aren't unique combinations.
dpb
on 27 May 2018
"Working" doesn't necessarily imply "correct"; whether you got the right answer or not depends on where the UNIQUE() values were and what happened to the x,y relationship from that point forward.
Simply removing the two "extra" points from the longer vector may make no difference for the specific point, may make only an insignificant difference, or could have a profound effect on the result; all depends on just what the data are and how the values were rearranged owing to the independent removal.
Paolo
on 27 May 2018
@dpb that's correct, I did state that I just removed the two points for testing purposes only. I just pointed out the nature of the error, Ismail will have to determine why the data he is using right results in incorrect values when using unique().
dpb
on 27 May 2018
Ismail (the OP) indicated it "was working"; response is to that comment.
As for the cause, I outlined the "why" of the differing lengths above as well as the manner in which it could be corrected.
Ismail Qeshta
on 27 May 2018
dpb
on 27 May 2018
Very risky move... :)
Ismail Qeshta
on 27 May 2018
Answers (0)
Categories
Find more on Interpolation 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!