"not equal to" in MATLAB

2,899 次查看(过去 30 天)
Mobile Computing
Mobile Computing 2019-6-14
回答: Prannoy 2023-6-4
How do I use "not equal to" symbol in MATLAB?
not_equal.png
The above code gives an error.
  2 个评论
Stephen23
Stephen23 2019-6-14
编辑:Stephen23 2019-6-14
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
Steven Lord 2019-6-14
FYI for Mobile Computing, the exclamation point (bang) has a different meaning in MATLAB.

请先登录,再进行评论。

回答(5 个)

Jan
Jan 2019-6-14
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
Manvi Goel 2019-6-14
You can use the ~ instead of ! for not in MATLAB
correct.png
  1 个评论
Guillaume
Guillaume 2019-6-14
"You can use..."
It's not you can. It's you have to.

请先登录,再进行评论。


Jack Rigby
Jack Rigby 2020-11-17
~=

Richard Payne
Richard Payne 2021-2-20
Is there hack to accept != ?
  1 个评论
Steven Lord
Steven Lord 2021-2-21
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
a = logical
0
b = 1 ~= 2
b = logical
1
!date
Sun Feb 21 00:19:13 UTC 2021

请先登录,再进行评论。


Prannoy
Prannoy 2023-6-4
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.

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by