Was it helpful?

Question

How to find the rank of a matrix in R?

R ProgrammingServer Side ProgrammingProgramming

The rank of a matrix is defined as the maximum number of linearly independent vectors in rows or columns. If we have a matrix with dimensions R x C, having R number of rows and C number of columns, and if R is less than C then the rank of the matrix would be R. To find the rank of a matrix in R, we can use rankMatrix function in Matrix package.

Loading Matrix package −

library(Matrix)

Example

 Live Demo

M1<-matrix(1:9,ncol=3)
M1

Output

[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
rankMatrix(M1)
[1] [1] 2

Example

 Live Demo

M2<-matrix(1:36,nrow=6)
M2

Output

    [,1] [,2] [,3] [,4] [,5] [,6]
[1,]  1    7   13   19   25   31
[2,]  2    8   14   20   26   32
[3,]  3    9   15   21   27   33
[4,]  4    10  16   22   28   34
[5,]  5    11  18   24   30   36
rankMatrix(M2)
[1] [1] 2

Example

 Live Demo

M3<-matrix(sample(0:9,100,replace=TRUE),nrow=20)
M3

Output

[,1] [,2] [,3] [,4] [,5]
[1,] 9 8 1 4 1
[2,] 5 3 0 6 1
[3,] 2 6 5 5 4
[4,] 5 8 3 7 5
[5,] 3 1 4 0 3
[6,] 1 5 5 3 5
[7,] 0 3 0 3 5
[8,] 1 9 4 0 4
[9,] 1 4 3 7 6
[10,] 1 3 6 1 4
[11,] 6 9 2 5 9
[12,] 7 0 2 8 1
[13,] 5 9 4 1 1
[14,] 9 1 1 6 4
[15,] 1 4 4 0 7
[16,] 6 0 9 2 7
[17,] 9 3 0 2 1
[18,] 9 6 0 7 2
[19,] 5 2 0 8 5
[20,] 8 8 4 0 4
rankMatrix(M3)
[1] [1] 5

Example

 Live Demo

M4<-matrix(sample(21:50,160,replace=TRUE),nrow=20)
M4

Output

    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 49   35   25 36 31 22 49 42
[2,] 45   33   21 39 37 26 41 37
[3,] 36   30   46 50 26 32 24 49
[4,] 33   39   31 39 37 50 50 32
[5,] 41   38   39 38 40 25 42 30
[6,] 34   49   26 34 24 34 42 26
[7,] 40   44   30 21 27 47 32 47
[8,] 35   27   21 25 37 28 48 27
[9,] 34   50   41 38 39 27 43 40
[10,] 21 41 50 28 32 38 22 25
[11,] 26  46 34 29 24 38 28 39
[12,] 36  41 36 47 49 48 49 31
[13,] 44  50 37 38 33 26 37 45
[14,] 31  22 47 37 47 41 23 45
[15,] 42  39 38 26 35 21 31 31
[16,] 50  29 40 47 48 34 23 29
[17,] 49  21 30 47 24 21 43 47
[18,] 27  22 25 34 45 28 49 45
[19,] 38  39 32 31 29 23 34 45
[20,] 45  37 31 43 40 27 29 36
rankMatrix(M4)
[1] [1] 8

Example

 Live Demo

M5<-matrix(sample(rnorm(20),25,replace=TRUE),nrow=5)
M5

Output

[,1] [,2] [,3] [,4] [,5]
[1,] 0.73475325 -1.2186186 -0.21496111 1.8642877 1.1133489
[2,] -0.09091262 -0.4513579 0.18346741 0.1834674 -0.4513579
[3,] 1.55003168 -0.1454168 0.28244025 0.5674004 1.5706586
[4,] -0.90693971 -1.2186186 -0.09091262 -0.1454168 1.0289755
[5,] -0.45135792 0.5674004 0.28244025 -0.2149611 1.5500317


rankMatrix(M5)
[1] [1] 5

Example

 Live Demo

M6<-matrix(sample(rexp(5,2),20,replace=TRUE),nrow=10)
M6

Output

[,1] [,2]
[1,] 0.2969787 3.5003481
[2,] 0.5462926 3.5003481
[3,] 1.0018821 0.5462926
[4,] 3.5003481 0.2969787
[5,] 1.0018821 0.2969787
[6,] 1.0018821 0.2969787
[7,] 1.0018821 0.5462926
[8,] 0.2133455 0.2133455
[9,] 0.5462926 3.5003481
[10,] 0.2969787 0.2133455


rankMatrix(M6)
[1] [1] 2
raja
Published on 09-Oct-2020 18:41:57
Advertisements
Was it helpful?
Not affiliated with Tutorialspoint
scroll top