Updated March 8, 2023
Introduction to Matlab not equal
The following article provides an outline for Matlab not equal. Matlab supports all types of operators such as arithmetic, logical, comparison, relational etc. In a relational operator group, there are various operations which decide the relation between two quantities. Relational operators are equal to (= =), not equal (~ =), greater than (>), less than (<) and so on. Every operator has two ways to implement in a program, one is by using syntax, and other one is by using operator symbol. Similarly, for Not equal operator is implemented in two ways: using the symbol ‘~=’ and using syntax ‘ne’. The output of both the methods will be the same.
Syntax:
var1 ~= var2
first variable name ~= second variable name
ne(var1,var2)
ne(first variable name ,second variable name)
How not equal Operator Work in Matlab?
- Not equal to operator compares two quantities or elements at a time and gives result in the form of true or false.
- If both the elements are the same, then not equal to operation fails, and it gives the result as false similarly if both the elements are not same then not equal to condition satisfies then will be true.
- True and False are represented in the form of once and zeros.
- If the result is true, it will be represented as ‘1’, and if the result is false, it will be represented as ‘0’.
Examples of Matlab not equal
Given below are the examples of Matlab not equal:
Example #1
Let us consider one simple example of one element. The first element ‘11’ is declared as ‘var 1’, and the second elements ‘29’ is declared as ‘var 2’. In example 1(a), not equal operator used by the symbolic method and in example 1(c), the same problem illustrated by using syntax ‘ne’. In this two examples, elements are different; therefore, we will get the result as 1. In example 1(b) and 1(d), elements are the same; therefore, we will get the result as 0 by using both symbolic and syntax methods, respectively.
Matlab code for Example 1(a):
Code:
clear all;
disp ('Output');
var 1 = 11
var 2 = 29
var1 ~ = var2
Output:
Matlab code for Example 1(b):
Code:
clc;
clear all;
disp ('Output');
var 1 = 30
var 2 = 30
var1 ~ = var2
Output:
Matlab code for Example 1(c):
Code:
clc;
clear all;
disp ('Output');
var 1 = 50
var 2 = 29
ne (var1, var2)
Output:
Matlab code for Example 1(d):
Code:
clc;
clear all;
disp ('Output');
var 1 = 30
var 2 = 30
ne (var1, var2)
Output:
Example #2
In the previous example, input data was in a single element(single dimension), but if the data has multiple elements then not equal to operation will compare every element from the database and gives output individually for each element, which is illustrated in example 2(a) by using both the methods. In example 2(b), we can see data is multi-dimensional (matrix); it has three rows and four columns, so not the equal operation will work on every element of the matrix, and it will give result in the form of three rows and four columns.
Matlab code for Example 2(a):
Code:
clc;
clear all;
var 1 = [34, 54, 65]
var 2 = [35, 54, 45]
disp ('Output by ~ = operator -');
var 1 ~ = var 2
disp ('Output by ne syntax- ');
ne (var 1, var 2)
Output:
Matlab code for Example 2(b):
Code:
clc;
clear all;
var 1 = [5 6 3 2 ; 3 5 6 7 ; 2 7 8 4]
var 2 =[4 5 3 1 ; 3 5 4 9 ; 6 5 3 4]
disp('Output by ~ = operator -');
var 1 ~ = var 2
disp ('Output by ne syntax- ');
ne ( var 1, var 2)
Output:
Example #3
In this example type of input, arguments are different. Previously we have seen simple integers now; we will consider a complex variable as elements. Here the input is multidimensional as well as each element is a complex variable. On such type of dataset, not equal operator compares real and imaginary parts of every element and gives results accordingly, illustrated in example 3.
Code:
clc;
clear all;
var 1 = [5 + 6i, 3 + 3i, 2 - i]
var2 = [ 6 + 7i, 3 + 3i, 6 - i]
disp('Output by ~ = operator -');
var 1 ~ = var 2
disp ('Output by ne syntax- ');
ne ( var 1,var 2)
Output:
Example #4
In this example, we will consider input in forms characters and strings. In example 4(a), first input is the word ‘hellomatlab’, and the second input is ‘m’, which is a single letter. The not equal operation will compare each element from a word with a single letter ‘m’, and it will give a result in the form of an array. And in example 4(b), the first input is an array of words which is compared with a single word, so not equal operator will compare all the words from the first input with the word ‘hello’, and it will also give output in the form of an array.
Matlab code for Example 4(a):
Code:
clc;
clear all;
var 1 = 'hellomatlab'
var 2 = 'm'
disp('Output by ~ = operator -');
var 1 ~ = var 2
disp('Output by ne syntax- ');
ne (var 1 ,var 2)
Output:
Matlab code for Example 4(b):
Code:
clc;
clear all;
var 1 = categorical({'hello', 'hi', 'hi', 'hello', 'hello', 'hi'})
var 2 = 'hello'
disp ('Output by ~= operator -');
var 1 ~ = var 2
disp ('Output by ne syntax- ');
ne (var 1,var 2)
Output:
Conclusion
In this article, we have seen various ways to use not equal operation in Matlab. The operator performs on each element of input irrespective of dimensionality of input data like single element, single dimension or multiple elements and multidimensionality. It performs on all data types such as integers, floating numbers, complex variables, characters and strings.
Recommended Articles
This is a guide to Matlab, not equal. Here we discuss the introduction, how not equal operator work in matlab and examples, respectively. You may also have a look at the following articles to learn more –