precision of function: UNIQUE
9 views (last 30 days)
Show older comments
dear all,
I am trying to use interp1 for a simple matrix, the first row has X coordinates and the second row Y coordinates from experimental data.
The problem now is that X coords have precision 0.0000 so the function interp1 faces problem giving the error below:
Error using griddedInterpolant
The grid vectors are not strictly monotonic increasing.
very nice, common one. i used the function unique to 'clean' the X-values but i think that it cannot recognize the difference up to specific precision so i get the same values back.
Does anyone know if i can choose the precision or any other trick for this one?
Thank you
0 Comments
Answers (2)
Star Strider
on 9 Dec 2022
That they are not monotonically increasing is not the same as their not being unique. That is a different problem.
If you know the approximate value you want to interpolate, do something like this —
x = 0:0.1:2*pi;
y = sin(x);
yq = 0.5; % Determine X-Values For This Y-Value
idxv = find(diff(sign(y-yq)));
for k = 1:numel(idxv)
idxrng = max(1,idxv(k)-1) : min(numel(x),idxv(k)+1);
xq(k) = interp1(y(idxrng), x(idxrng), yq);
end
xq % X-Values Where 'y = yq'
figure
plot(x, y)
hold on
plot(xq, ones(size(xq))*yq, 'rs')
hold off
grid
.
0 Comments
See Also
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!