Updated March 4, 2023
Introduction to Absolute Value Matlab
Absolute value of a number gives us its distance form 0 on the number line. Absolute value does not consider the direction in which the number lies, and so absolute values are never negative. For vectors, absolute value is the magnitude of the vector. The absolute value for complex number is given by distance of the number from the origin in a complex plane. In Matlab, we use ‘absolute function’ to get the absolute value of any number or vector.
Syntax of Absolute Value Function in Matlab:
X = abs (S)
Description:
- X = abs (S): It is used to get the absolute value for every element in the input S.
- If the input array S has complex elements, then abs (S) function will return ‘complex magnitude’.
Examples of Absolute Value Matlab
Given below are the codes to calculate the absolute value in Matlab using ‘abs (S) function’:
Example #1
In this example, we will take a simple scalar and will find its absolute value using abs (S) function.
We will follow the following 2 steps:
- Take a scalar.
- Pass it as an argument to the absolute value function.
Code:
S = -10
[Initializing the scalar ‘S’]X = abs (S)
[Passing the scalar as an input to the absolute function] [Please note that, we can directly pass the scalar value as input to the abs function like, abs (-10)] [Mathematically, the absolute value of -10 is 10]Input:
S = -10
X = abs (S)
Output:
As we can see in the output, we have obtained absolute value of our input scalar as 10, which is same as expected by us.
Let us now see how the code for absolute value looks like in Matlab if we want to compute absolute value of a complex number.
[Please note that, mathematically, the complex magnitude or absolute value of a complex number (a + ib) is given by square root of (a^2 + b^2)].Example #2
In this example, we will take a complex number and will find its absolute value using abs (S) function.
We will follow the following 2 steps:
- Take a complex number.
- Pass it as an argument to the absolute value function.
Code:
S = 6 + 8i
[Initializing the complex number ‘S’]X = abs (S)
[Passing the complex number as an input to the absolute function] [Please note that, we can directly pass the complex value as input to the abs function like, abs (6 + 8i)] [Mathematically, the absolute value of (6 + 8i) is 10]Input:
S = 6 + 8i
X = abs (S)
Output:
As we can see in the output, we have obtained absolute value of our input complex number as 10, which is same as expected by us.
Let us now see how the code for absolute value looks like in Matlab if we want to compute absolute value for vectors.
Example #3
In this example, we will take an array of vectors and will find its absolute value using abs (S) function.
We will follow the following 2 steps:
- Create the input array.
- Pass it as an argument to the absolute value function.
Code:
S = [1.3 -0.56 8.3; 3 -5 6.2] [Initializing the input array ‘S’]
X = abs (S)
[Passing the array as an input to the absolute function] [Mathematically, the absolute value of [1.3 -0.56 8.3; 3 -5 6.2] is [1.3000 0.5600 8.3000; 3.0000 5.0000 6.2000]Input:
S = [1.3 -0.56 8.3; 3 -5 6.2]
X = abs (S)
Output:
As we can see in the output, we have obtained absolute value of our input array of vectors as expected by us.
Let us now see how the code for absolute value looks like in Matlab if we want to compute absolute value for an array of complex numbers.
Example #4
In this example, we will take an array of complex numbers and will find its absolute value using abs (S) function.
We will follow the following 2 steps:
- Create an array of complex number.
- Pass it as an argument to the absolute value function.
Code:
S = [6+8i 3+4i 5+2i; 2+3i 4+1i 5+4i] [Creating the array of complex numbers]
X = abs (S)
[Passing the array as an input to the absolute function] [abs (S) will find the absolute value of every element of the array]Input:
S = [6+8i 3+4i 5+2i; 2+3i 4+1i 5+4i]
X = abs (S)
Output:
As we can see in the output, we have obtained absolute value of our input array of complex numbers as expected by us.
Conclusion
Absolute value function can be used in Matlab to get the absolute value of any scalar or vector. We can also use the same function to get the complex magnitude of complex numbers.
Recommended Articles
This is a guide to Absolute Value Matlab. Here we discuss the introduction to Absolute Value Matlab along with examples respectively. You may also have a look at the following articles to learn more –