comparing floating point numbers for equality
63 次查看(过去 30 天)
显示 更早的评论
Assign matchFound with true if the difference between firstSample and secondSample is within thresholdValue.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1276955/image.png)
And the code to call the function should be: IsMatch((4/5), 0.8, 0.0001)
I am having trouble understanding. I know I should at least have abs(firstSample-secondSample)
Can someone break it down for me?
3 个评论
cdawg
2023-1-28
Assuming this is homework and you can't use a built in function...
You are correct, abs(firstSample - secondSample) is a good start. Your function also returns one of two options: true or false. Try using an if statement to decipher between the two options (when do you return true and when do you return false?)
You might also want to experiment in the command line or a blank script (getting errors within a function can be frustrating because it doesn't give you any output).
dpb
2023-1-28
"...are correct, abs(firstSample - secondSample) is a good start. Your function also returns one of two options: true or false. Try using an if statement to decipher between the two options"
This does NOT require an explicit if construct (and an optimum solution should not include one).
HINT: As @Caitlin O'Brien suggests, try some expressions at the command line and see what you get for given sets of values and the threshold. NOTA BENE: your expression so far doesn't make use of the threshold value; what part would it play in making the ultimate result? HINT SECOND: Think about how you would solve the problem with pencil and paper to write down the answer on a pop quiz...
回答(1 个)
Image Analyst
2023-1-28
The function inside would be a simple one line statement like
something = abs(something - something) < something;
You should be able to figure it out what to replace something by. I mean lines 6 and 7 of your code practically spell it out explicitly for you. Or, like @dpb said, you can also use ismembertol in a single line of code with your function just being a simple wrapper for ismembertol.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!