Comparing two numbers by using IF in Matlab

68 views (last 30 days)
I want to compare two numbers (x, and y), if they are equal or not.
However, even if these two numbers are equal, if condition in Matlab does not recognize them as equal numbers.
How I can make if statement recognize these two numbers if they are equal?
if x == y
'Not Equal'

Accepted Answer

Iain on 15 Oct 2014
The code is correct.
The problem is that x - y is not precisely 0. The magnitude of the difference will be less than 1e-5
You have two options, define y to the 16th significant figure, or allow a tolerance like this:
if abs(x-y) < tolerance

More Answers (2)

Stephen Carter
Stephen Carter on 15 Oct 2014
They are not equal:
>> format long
>> x
x =
>> y
y =

Image Analyst
Image Analyst on 15 Oct 2014
See the FAQ for an explanation and some code to make it do what you want:


Find more on Introduction to Installation and Licensing 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!