Is there a function in MATLAB for executing code with a tolerance value?

  • Context: MATLAB 
  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Matlab Tolerance
Click For Summary
SUMMARY

The discussion addresses the need for a MATLAB function to execute code based on a tolerance value rather than an exact match. Users suggested using the absolute value function to compare a variable against a target with a specified tolerance. Specifically, the code snippet provided utilizes the absolute difference to determine if the condition is met. Additionally, the use of the 'find' function allows for identifying the index of the first occurrence within an array that meets the tolerance criteria.

PREREQUISITES
  • Understanding of MATLAB programming language
  • Familiarity with conditional statements in MATLAB
  • Knowledge of absolute value functions
  • Experience with array manipulation in MATLAB
NEXT STEPS
  • Research MATLAB's 'abs' function for numerical comparisons
  • Explore the 'find' function in MATLAB for locating elements in arrays
  • Learn about implementing tolerance-based conditions in MATLAB
  • Investigate MATLAB's built-in functions for numerical analysis
USEFUL FOR

This discussion is beneficial for MATLAB programmers, data analysts, and engineers who require precise numerical comparisons and conditional execution based on tolerance values in their code.

Saladsamurai
Messages
3,009
Reaction score
7
OK! I am not sure how to word this, but here goes:

Let's say I want to write a conditional like:

Code:
for i = 1:100
if myVar(i) == 4
do some stuff
break
end

The problem is...myVar might not ever equal exactly 4. It might be that myVar == 4.001 and that is fine; I would like it to execute the code.

Now, I know that I could give it a range like:

Code:
for i = 1:100
if myVar(i) < 4.002 && myVar(i) > 3.998
do some stuff
break
end

But is there a better way? Is there a function in MATLAB that takes my target value of 4 and my tolerance of .002 as its arguments? Or any other better ways?

Thanks!
 
Physics news on Phys.org
I don't see anything wrong with the way you are doing it. If you want to reduce your two conditions to one condition, you can use absolute value like this:

Code:
target = 4; % whatever target you want
tol = 0.002; % whatever tolerance you want

if abs(myVar(i)-target) <= tol
.
.
.

or if you'd like to get rid of the loop and myVar is an array of values, you can do something like this:

Code:
index = find(abs(myVar-target) <= tol,1)
.
.
.

so that index would return the index of the first value in the myVar array to meet your criteria - but that may have nothing to do with what you are trying to do.

Other than that, I'm not aware of a function that does what you are asking - but if it exists, its code probably looks a lot like what you've written. Hope that helped a little.
 

Similar threads

Replies
6
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K