Error using matlab.int​ernal.math​.interp1, Sample points must be unique.

262 visualizzazioni (ultimi 30 giorni)
I am getting this error when I run the attached matlab code. I couldn't figure out how to solve it and I would be very happy if you can help me.
Thanks in advance.
  12 Commenti
dpb
dpb il 24 Ott 2023
Modificato: dpb il 25 Ott 2023
Good sidebar comment @Steven Lord -- I wasn't aware that 'MarkerIndices' had been added to line properties....or maybe it's always been there and I just didn't recall it; I know I've combed the list in its entirety before.

Accedi per commentare.

Risposte (2)

Rik
Rik il 24 Ott 2023
Modificato: Rik il 24 Ott 2023
interp1 is not intended to fit equations. There are other tools to do that. If you insist on using this function, you will either have to calculate some consensus value for each unique x (e.g. the mean, the median, the maximum, the minimum, ...), or you should adjust the x values slightly so each value becomes unique.
Note that the order of your x-values suddenly matters for the second option, because each element will have a different offset based on the position.
x = [1 1 1 2 3];
y = [0 1 2 3 4];
xq = 1:3;
[x2,y2]=AdjustWithMedian(x,y)
x2 = 1×3
1 2 3
y2 = 1×3
1 3 4
[x3,y3]=AdjustWithEps(x,y)
x3 = 1×5
1.0000 1.0000 1.0000 2.0000 3.0000
y3 = 1×5
0 1 2 3 4
[x4,y4]=AdjustWithPolyfit(x,y)
x4 = 1×3
1 2 3
y4 = 1×3
1.0625 2.6250 4.1875
plot(x,y,'b*','DisplayName','raw data'),axis([0.5 3.5 -0.5 4.5])
hold on
plot(xq,interp1(x2,y2,xq),'m--o','DisplayName','Adjusted with median')
plot(xq,interp1(x3,y3,xq),'k:o','DisplayName','Adjusted with eps')
plot(xq,interp1(x4,y4,xq),'g-.o','DisplayName','Adjusted with polyfit')
hold off
legend('Location','SouthEast')
function [x_adj,y]=AdjustWithEps(x,y)
% Add a different amount to each x-value to make them unique.
% The sum of the shift should be equal to 0 to avoid an overall drift.
shift = 1:numel(x);
shift = shift - mean(shift);
x_adj = x(:) + shift(:)*eps;
x_adj = reshape(x_adj,size(x));
end
function [new_x,new_y]=AdjustWithMedian(x,y)
% For each unique x, only keep the median y-value
[new_x,ignore,ind] = unique(x);
new_y = accumarray(ind,y,[numel(new_x) 1],@median);
if isrow(x),new_y = new_y.';end % Keep directions consistent
end
function [x,y]=AdjustWithPolyfit(x,y)
p=polyfit(x,y,1);
x=unique(x);
y=polyval(p,x);
end

dpb
dpb il 24 Ott 2023
Modificato: dpb il 24 Ott 2023
As the follow on comment above shows, you can safely ignore the duplicate points and retain the original function -- use
[~,iu]=unique(data(:,1));
data=data(iu,:);
in place of the full data array to reduce it to the set of unique times and associated values; then pass the resulting time, response vectors to the function instead and all should work just fine...although there might be a question regarding there being two transients in the overall trace? Or is this an input concentration to be modelled its dispersion, maybe, in which case it could be intended and correct--we don't know anything about the problem trying to solve, just that there were duplicated times in this input trace that aren't allowed by the construction of the function.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by