MATLAB Norm of Rows of a Matrix

Ammar Ali Dec 22, 2023
  1. Use the vecnorm() Function to Find the Norm of Rows of a Matrix in MATLAB
  2. Use the sqrt() and sum() Function to Find the Norm of Rows of a Matrix in MATLAB
  3. Use the norm() Functions With a Loop to Find the Norm of Rows of a Matrix in MATLAB
  4. Conclusion
MATLAB Norm of Rows of a Matrix

In the world of math and MATLAB, understanding how to find the norm of each row in a matrix is crucial. This tutorial will discuss different methods to find the norm of each row of a matrix in MATLAB.

Use the vecnorm() Function to Find the Norm of Rows of a Matrix in MATLAB

The vecnorm() function in MATLAB is used to calculate the vector norms of vectors or matrix rows. It allows you to compute various types of norms, such as the Euclidean norm, along specified dimensions, and it provides a concise and efficient alternative to using loops for norm calculations.

Using the vecnorm() function in MATLAB is a convenient and efficient way to find the norm of each row of a matrix.

Basic Syntax:

output = vecnorm(matrix, normType, dimension);

Parameters:

  • matrix: The input data, which can be a vector, matrix, or N-dimensional array.
  • normType: The type of norm to be computed, such as 2 for the Euclidean norm or inf for the infinity norm.
  • dimension (optional): Specifies whether the norm is calculated along each column (1, default) or each row (2).

In the syntax above, If dimension is set to 1(default), it calculates the norm along each column, resulting in a row vector of norms. If dimension is set to 2, it calculates the norm along each row, resulting in a column vector of norms.

Lastly, the calculated norms are stored in the variable specified by output.

Example Code:

m = magic(3)
n1 = vecnorm(m)
n2 = vecnorm(m,2,2)

Output:

m =

     8     1     6
     3     5     7
     4     9     2


n1 =

    9.4340   10.3441    9.4340


n2 =

   10.0499
    9.1104
   10.0499

In the code, we initiated a 3x3 magic matrix, denoted as m, in MATLAB using the magic() function, and we used vecnorm() to calculate the default Euclidean norm along each column, storing results in n1. Then, by specifying norm type 2 and dimension 2, we calculated the Euclidean norm along each row, saving results in n2.

The output provides two sets of norm values for a 3x3 magic matrix, m, using vecnorm() in MATLAB. The first set, n1, represents Euclidean norms along columns, while the second set, n2, represents norms along rows.

Use the sqrt() and sum() Function to Find the Norm of Rows of a Matrix in MATLAB

The sqrt() function in MATLAB is used to compute the square root of each element in an array. It can be applied element-wise to a matrix or vector, and the sum() function in MATLAB calculates the sum of elements along a specified dimension or of all elements in a matrix or vector.

Using the sqrt() and sum() functions together is another way to find the norm of each row of a matrix in MATLAB. This approach involves squaring each element in a row, summing the squared values, and then taking the square root of the sum.

Example Code:

m = magic(3)
n1 = sqrt(sum(m.^2,2))

Output:

m =

     8     1     6
     3     5     7
     4     9     2


n1 =

   10.0499
    9.1104
   10.0499

In the code, we initiated a 3x3 magic matrix, denoted as m, in MATLAB using the magic() function. Subsequently, we employed a combination of functions to calculate the Euclidean norm along each row of the matrix.

Specifically, we squared each element of m using the .^2 element-wise operation, followed by using the sum() function along dimension 2 to sum the squared values for each row. Finally, we applied the sqrt() function to obtain the square root of each sum, resulting in a column vector of norm values stored in the variable n1.

The output, stored in n1, is a column vector representing the Euclidean norms for each row of the 3x3 magic matrix, m. The values, approximately 10.0499, 9.1104, and 10.0499, indicate the length or magnitude of each row.

Use the norm() Functions With a Loop to Find the Norm of Rows of a Matrix in MATLAB

The norm() function in MATLAB is a versatile function used for calculating various vector and matrix norms. It computes the magnitude or size of a vector or matrix, and it supports different types of norms.

Syntax:

output = norm(X, p);

Parameters:

  • X: This parameter represents the input data for which the norm is calculated. It can be a vector, matrix, or a multidimensional array.
  • p(optional): Specifies the type of norm to be computed.

In the syntax above, the norm() function calculates the norm of the input data X based on the specified norm type p. The behavior of the function depends on the dimensionality of X.

For vectors (1D arrays), it calculates the vector norm. On the other hand, for matrices (2D arrays), it calculates the matrix norm.

If the p parameter is not specified, the default behavior is to compute the Euclidean norm (p = 2).

Using the norm function with a loop is a method to find the norm of each row of a matrix in MATLAB. This approach involves iterating through each row of the matrix and applying the norm function to calculate the norm of that specific row.

Example Code:

m = magic(3)
[rows, ~] = size(m);
n2 = zeros(rows, 1);

for i = 1:rows
    n2(i) = norm(m(i, :));
end

n2

Output:

m =

   8   1   6
   3   5   7
   4   9   2

n2 =

   10.0499
    9.1104
   10.0499

In the code, we initialized a 3x3 magic matrix, denoted as m, in MATLAB using the magic() function. Next, we determined the number of rows in m using the size() function, assigning the result to the variable rows.

We then created a column vector, n2, initialized with zeros and matching the number of rows in m. Utilizing a for loop, we iterated through each row of the matrix and inside the loop, we employed the norm() function to calculate the Euclidean norm for each row, and the resulting norm values were stored in the n2 vector.

The output is a column vector, denoted as n2, containing the Euclidean norms calculated for each row of the 3x3 magic matrix, m and the specific values in this vector are approximately 10.0499, 9.1104, and 10.0499. The loop efficiently iterated through each row, applying the norm() function, and stored the resulting values in the n2 vector.

Conclusion

In conclusion, we’ve navigated through diverse methods for determining the norm of each row within a matrix in MATLAB. The vecnorm() function emerged as an efficient and flexible tool, allowing us to compute vector norms along specified dimensions without the need for loops.

Furthermore, we explored an alternative approach using the sqrt() and sum() functions, offering simplicity in norm calculations. Additionally, we delved into the traditional usage of the norm() function, combined with a loop, showcasing its adaptability in computing various vector and matrix norms.

Whether opting for the streamlined vecnorm() or combining functions for efficiency, MATLAB provides a range of tools to cater to different preferences and necessities in matrix norm computations.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - MATLAB Matrix