Can someone explain "tol" for me in ismembertol(A,B,tol) to see if I could use it in this context?

67 views (last 30 days)
I am working on a project, and I came accross the ismembertol function. "x" is a 61x61 double, and I want to extract numbers closest to the values in the xline vector. Can I use ismembertol(a,b,tol) for this? Is there a better way? Maybe using logical operations? What is the "tol" part, what does it do, and how can it be applied?
%if not exact match
%lets say we are given matrices x,y, and z by the map
xline = (0.0000:0.050:3.0000);
tol = eps(0.5); %idk what I'm doing here or what the purpose of this is. I saw it as an example on MathWorks
idx2 = find(ismembertol(x,xvalues,tol));

Accepted Answer

Steven Lord
Steven Lord on 5 Jun 2019
ismembertol(A, B) returns an array the same size as A. Each element of the output is true if the corresponding element of A is "close enough" to an element of B. The tol input argument lets you specify what constitutes "close enough".
  2 Comments
Naomi Amuzie
Naomi Amuzie on 7 Jun 2019
Based on that, I have tol = esp(0.5). What is esp? and does that mean all values that are 0.5 less and 0.5 more?
Steven Lord
Steven Lord on 7 Jun 2019
eps represents the distance from a number to the next largest double-precision number. So eps(0.5) is what you need to add to 0.5 to get to the next larger number. It's quite small. Given the data in your xline vector, the default tolerance of 1e-12 is probably sufficient.
Let's use your sample data.
xline = (0.0000:0.050:3.0000)
While xline looks like it contains the value 1.45 it actually doesn't.
any(xline == 1.45)
If you were to display the element of xline that looks like it should be equal to 1.45 and 1.45 itself using the hex format to show the actual bits with which the data is stored, you'll see that it differs in the last bit.
>> format hex
>> [xline(30); 1.45]
ans =
3ff7333333333334
3ff7333333333333
>> format % Change back to the default display format
Using ismembertol with the default tolerance does find element 30 of xline, as the difference between it and 1.45 is smaller than the tolerance.
ismembertol(xline, 1.45, 1e-12)
xline(ismembertol(xline, 1.45, 1e-12))

Sign in to comment.

More Answers (0)

Categories

Find more on Numeric Types 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!