precision of function: UNIQUE

9 views (last 30 days)
Ilias Psilakis
Ilias Psilakis on 9 Dec 2022
Answered: Star Strider on 9 Dec 2022
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

Answers (2)

Star Strider
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'
xq = 1×2
0.5241 2.6176
figure
plot(x, y)
hold on
plot(xq, ones(size(xq))*yq, 'rs')
hold off
grid
.

John D'Errico
John D'Errico on 9 Dec 2022
help uniquetol
UNIQUETOL Set unique within a tolerance. UNIQUETOL is similar to unique. Whereas unique performs exact comparisons, UNIQUETOL performs comparisons using a tolerance. C = UNIQUETOL(A,TOL) returns the unique values in A using tolerance TOL. Each value of C is within tolerance of one value of A, but no two elements in C are within tolerance of each other. C is sorted in ascending order. UNIQUETOL scales the TOL input based on that magnitude of the data, so that two values u and v are within tolerance if: abs(u-v) <= TOL*max(abs(A(:))) C = UNIQUETOL(A) uses a default tolerance of 1e-6 for single precision inputs and 1e-12 for double precision inputs. [C,IA,IC] = UNIQUETOL(A) returns index vectors IA and IC such that: C = A(IA) A ~ C(IC) (or A(:) ~ C(IC), if A is a matrix) where ~ means the values are within tolerance of each other. C = UNIQUETOL(...,OCCURRENCE) specifies how UNIQUETOL extracts unique values from A. In both cases, C is returned in ascending order. OCCURRENCE can be: 'lowest' - (default) Start with the lowest value in A, then repeatedly find the next lowest value of A that is not within tolerance of any other value in C. 'highest' - Start with the highest value in A, then repeatedly find the next highest value of A that is not within tolerance of any other value in C. [...] = UNIQUETOL(...,'Name1',Value1,'Name2',Value2,...) specifies one or more of the following Name/Value pair arguments using any of the previous syntaxes: OutputAllIndices - When true, this returns a cell-array IALL (instead of IA) as the second output, indicating all the indices for elements in A that are within tolerance of a value in C. The default value is false. ByRows - When true, this returns a matrix C of the same size as A such that every row of A is within tolerance of a row in C. For the rows case, two rows u and v are within tolerance if: all(abs(u-v) <= TOL*max(abs(A),[],1)) Use this to find rows that are the same, within tolerance. For two rows to be within tolerance each column has to be within tolerance. The default value is false. DataScale - A scalar which changes the tolerance test to be: abs(u-v) <= TOL*DS Specify a value DS if you want to specify an absolute tolerance. When used together with the ByRows option, the DataScale value may also be a vector. In that case, each element of the vector specifies DS for a corresponding column in A. If a value in the DataScale vector is Inf, then UNIQUETOL ignores the corresponding column in A. PreserveRange - When true, the minimum and maximum values of C are equal to the minimum and maximum values of A. If the minimum and maximum values of A are within tolerance of each other, UNIQUETOL returns only one of the values. Example: % A is a matrix of real values A = [0.05, 0.11, 0.18; 0.18, 0.21, 0.29; 0.34, 0.36, 0.41; 0.46, 0.52, 0.76; 0.82, 0.91, 1.00]; % The entries of B are the same as the entries of A within round-off error. B = log10(10.^A); % A and B differ by small amounts A-B % unique uses exact equality, most of the rows in A are not matched % with rows in B unique([A;B],'rows') % By default UNIQUETOL uses a small tolerance and the rows will be matched uniquetol([A;B],'ByRows',true) See also: UNIQUE, ISMEMBERTOL Documentation for uniquetol doc uniquetol Other uses of uniquetol gpuArray/uniquetol

Categories

Find more on Interpolation in Help Center and File Exchange

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!