Matlab

How to Use Not Equal in MATLAB?

MATLAB supports relational operators for comparing two quantities. These operations include greater than, less than, equal to, and, not equal. Not equal relational operator allows us to determine the inequality between two quantities. This blog will discuss how to utilize the not equal operator in MATLAB using some examples.

How to Implement Not Equal Operator in MATLAB?

The not equal or ~= operator in MATLAB is used for comparing two values, vectors, matrices, or arrays by returning an array having logical values for 1 and 0. The implementation of this operator uses both the operator “~=” and the syntax ne(). Both of these ways will yield the same result.

A ~= B

ne(A,B)

Here,

A ~= B yields a logical array or table of logical values, where each element is logical 1 (true) if inputs A and B are not equal and logical 0 (false) otherwise. The test compares the real and imaginary elements of numerical arrays.

A different technique for ~= is to use ne(A, B), however, this is implemented rarely.

Examples

Consider some examples to demonstrate the functionality of the not-equal operator in MATLAB.

Example 1

The given MATLAB code uses the ~= operator for comparing the given two values x and y.

x = 5;

y = 9;

x ~= y

After executing the above code, we will receive a logical value ‘1’ because the specified condition is satisfied.

Example 2

In this example, we use the ne() function for comparing the given two matrices x and y.

x = eye(3);

y = ones(3);

ne(x, y)

Conclusion

The not-equal operator in MATLAB allows us to determine the inequality between two quantities by returning an array having logical values for 1 and 0. The implementation of this operator uses both the operator “~=” and the syntax ne(). Both of these ways will yield the same result. This tutorial discovered how to use the not equal operator in MATLAB.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.