Interp1 is not working after applying unique because of rounding off
2 views (last 30 days)
Show older comments
Govind Sankar Madhavan Pillai Ramachandran Nair
on 20 Jun 2025
Commented: Govind Sankar Madhavan Pillai Ramachandran Nair
on 20 Jun 2025
I have a double array with many values. The first value is 3.569990000000001, the next multiple values is 3.570000000000001, then comes 3.570010000000001 and it works like this. I can see only these long values which i click on that cell, otherwise every value is being displayed as 3.57. Now I applied unique, so the multiple 3.570000000000001 is removed and there is now only one 3.570000000000001 and again and so on, but still all values are seen as 3.57. So if compared the first two values to see if it equal it is showing that they are not equal even though me as the user sees both values as 3.57. Now here comes the problem. I need to use interp1 on these values, but here interp1 is not reading them as 3.569990000000001 and 3.570000000000001. Interp1 is reading both of them as 3.57 and is giving me an error saying values need to be unique. I dont know what to do. I tried googling like converting to format long or something like that, i couldnt find anything. What can I do. Thank you.
0 Comments
Accepted Answer
More Answers (1)
Steven Lord
on 20 Jun 2025
I need to use interp1 on these values, but here interp1 is not reading them as 3.569990000000001 and 3.570000000000001. Interp1 is reading both of them as 3.57 and is giving me an error saying values need to be unique.
Please show a small sample of code including the full and exact text of the error message. That's not the behavior I'm seeing when I hard-code the values you say are in your vector. If that code requires some data, please also show a small sample of data we can use to run that code.
x = [3.569990000000001, 3.570000000000001]
uniqueX = unique(x)
y = [2, 4]
z = interp1(x, y, 3.57)
As Torsten said, if you want to remove the values that are "close enough" use uniquetol.
difference = diff(x)
tolerance = 1e-4;
uniqueXCloseEnough = uniquetol(x, tolerance)
See Also
Categories
Find more on Logical 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!