![]() |
A matrix is an arrangement of data in a row- and column-wise fashion or a matrix is nothing but a set of particular kinds of data like numbers. It has many applications in mathematics, Physics, engineering, etc. The number of rows and columns defines the matrix size called an order. For example, if the matrix has m rows and n columns, the order of the matrix would be m x n. A matrix in R Programming Language is a 2D array that can store numeric, character, or logical data.
Output: [,1] [,2] [,3] Rank of the Matrix in R Programming LanguageThe rank of matrix is used in linear algebra to describe the maximum number of linearly independent rows or columns of the matrix. The rank of matrix is written as rank(A) for a matrix A. Rank of matrix is a basic concept of linear algebra which provides certain useful information about properties and behavior of matrix. It is trying to measure up the “dimensionality” or number of pieces in information that are actually independent.
For an m x n matrix:
Rank of a matrix is of great significance
Methods to find Rank of a MatrixUsing Matrix PackageThere is function called rankMatrix which comes under the Matrix Package. This package has a special class for matrices that contains multiple functions for various operations. Now lets work out on some coding part.
Output: [1] 2 Using qr function from base RThe qr function of R performs the QR decomposition of a matrix. Q is the orthogonal matrix,, and R is the upper triangular matrix. Here we calculate rank by counting the number of non-zero diagonal elements in the upper triangular matrix obtained from the decomposition.
Output: [1] 2 The result of the function is a list with two members – component the $qr and rank of the matrix $rank. So using the latter one gives the rank of the matrix. Using svd (Singular Value Decomposition)SVD is method of factorization that breaks a matrix into 3 matrices and extracts the singular values. It makes calculating rank very easy which can be calculated by finding the count of non-zero singular values.
Output: [1] 4 SVD result list consists of 3 elements: $u ’ $d ‘, ‘ $v ‘ which are the left singular vectors, the singular values, and the right singular vectors respectively. Using svd(our_matrix)$d > 1e-10 ,a logical vector is created here, where the check occurs if each singular vector is greater than 1e-10 . The above argument which is placed into the sum() at the end counts the rank. Using eigenvalues and eigenvectorsEigenvalues and Eigenvectors are used in the analysis of linear transformations. The rank of the matrix can be determined by counting the number of non-zero eigenvalues.
Output: [1] 2 eigen(our_matrix) gives a list containing ‘$values’, ‘$vectors’. eigen(your_matrix)$values filters the values from the list. The argument inside sum function, takes the absolute values of the previous step’s eigenvalues, compares them with the threshold of 1e-10, and forms a logical vector. The sum function calculates the rank by summing up the true value, which is 1 in a logical vector. Using LinAlg packageThe LinAlg package contains a function called the MatrixRank function, which directly calculates the rank of a matrix. called theSyntax and parametersthe syntax:
Output: [1] 2 |
Reffered: https://www.geeksforgeeks.org
R Language |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |