"not equal to" in MATLAB
Show older comments
How do I use "not equal to" symbol in MATLAB?

The above code gives an error.
2 Comments
When I searched the internet for "MATLAB not equal" this was the first link returned:
which shows all of the element-wise logical operators, with links at the bottom to the relevant pages, e.g.:
Did you try looking at the MATLAB documentation?
Steven Lord
on 14 Jun 2019
Answers (5)
Jan
on 14 Jun 2019
Several possible ways:
if 1 ~= 2
if not(1 == 2)
if ~(1==2)
if ne(1, 2)
if ~eq(1, 2)
if 1 - 2 % Implicitly evaluated: all(1 - 2)
Manvi Goel
on 14 Jun 2019
3 votes
You can use the ~ instead of ! for not in MATLAB

1 Comment
Guillaume
on 14 Jun 2019
"You can use..."
It's not you can. It's you have to.
Jack Rigby
on 17 Nov 2020
0 votes
~=
Richard Payne
on 20 Feb 2021
0 votes
Is there hack to accept != ?
1 Comment
No. The ! operator means whatever follows will be passed to the operating system (assuming it is the first part of the command, otherwise it is an error.) You cannot change that definition. The ~ operator means logical negation, and the ~= operator means not equals.
a = ~true
b = 1 ~= 2
!date
Prannoy
on 4 Jun 2023
0 votes
In MATLAB, the ! operator has a separate meaning. '~' is used instead as a logical NOT operator. This means that "not equal to" will be represented by "~=" in MATLAB. I hope that clears your doubt.
Categories
Find more on Operators and Elementary Operations 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!