unique function usage at interp1 interpolation

14 views (last 30 days)
Hello, i have imported 2D data from the attached CSV as x_data and y_data.
i have used interp1 function as shown bellow to interpolated my descrete samples into continues function as shown in the code bellow.
Matlab gave me "The grid vectors must contain unique points." error.
I have tried to solve it using 'unique' function as shown bellow but its not working,
Where did i go wrong?
Thanks.
plot(x_data,y_data)
[x, index] = unique(x);
coef_fun = @(xq) interp1(x_data, y_data(index), xq);
xq = linspace(3.5,23,100000);
plot(xq, coef_fun(xq))
title('interp1')

Answers (2)

KSSV
KSSV on 7 May 2020
num = xlsread("DEfault Dataset4.csv") ;
x_data = num(:,1) ;
y_data = num(:,2) ;
plot(x_data,y_data)
[x, index] = unique(x_data);
y = y_data(index) ;
xq = linspace(min(x),max(x),100000);
yq = interp1(x,y,xq) ;
plot(xq,yq)
title('interp1')

Steven Lord
Steven Lord on 7 May 2020
You make the elements in x unique (though maybe you intended to make x contain the unique data from x_data? That's not what you wrote.) but then you call interp1 with x_data as the X coordinates. As this code is written there's no guarantee that x_data contains only unique values.

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!