2. After the Advance

Magnitude in Matlab

MATLAB is a programming environment that is interactive and is used in scientific computing. It is extensively used in a lot of technical fields where problem-solving, data analysis, algorithm development, and experimentation is required. The software which is discipline-specific is extensively written using MATLAB. In this article, we will study how to compute the magnitude of a vector or an array of vectors in MATLAB. There are mainly 2 functions that we can use to compute magnitude in MATLAB. The correct function to be used depends upon the type of input that we have and our expected output.

Functions to Calculate Magnitude in Matlab

Let us understand how to use this one by one with the help of different examples:

1. norm function


norm (A)

Explanation: norm (A) is used to calculate the 2-norm or in other words, vector magnitude of the input ‘A’. By default, the function calculates 2-norm, which can be changed if we have a different requirement bypassing the required norm in the argument.

2. abs function


Magnitude = abs (A)

Explanation: abs (A) will return absolute value or the magnitude of every element of the input array ‘A’. If the input ‘A’ is complex, then the abs function will return to a complex magnitude. Please recall that complex magnitude for a complex number X + Yi is the square root of (X^2 + Y^2).

Examples to Implement Magnitude Matlab

Below are examples mentioned:

Example 1:

In the first example, we will compute vector magnitude using default values, i.e, the 2-norm.


A = [3 4 5, 5 4 1, 2 4 1] Magnitude = norm (A)


vector magnitude

Example 2:

Let us now use the norm function to calculate the 3r d norm vector magnitude using the norm function.


A = [3 4 5, 5 4 1, 2 4 1] Magnitude = norm (A, 3)


3r d norm

Example 3:

In the first example, we will take an array of positive and negative values and will see how the code for computing the magnitude looks like.


A = [-0.32   1.34   -0.43   -1.3;   2  3.09   4.3   -2.43;   -2.45   1.4   0   -3] Magnitude = abs (A)


array of positive and negative values

Example 4:

In this example, we will see how the code for calculating complex magnitude using abs function looks like in MATLAB.


A = 8 + 6i
Magnitude = abs (A)


magnitude matlab4

Leave a Reply

Your email address will not be published. Required fields are marked *