   My Coding > Software > R > R matrix

# R matrix

Matrix in R are in fact rectangular array. It is possible to initialize matrix by reshaping vector into a two dimensional structure

In the following code we will combine 6 elements of vector into 3 rows and 2 columns. And we will fill it by columns (byrow=FALSE parameter)

``````
> M <- matrix(c(1,2,3,10,10,30), nrow=3, ncol=2, byrow=F)
> M
[,1] [,2]
[1,]    1   10
[2,]    2   10
[3,]    3   30``````

It is possible to access to individual element, to any row or column:

``````
> M[1,2] # individual element
 10
> M[1,] # Row
  1 10
> M[,2] # Column
 10 10 30``````

It is possible to convert matrix back to vector by function c()

``````
> b <- c(B)
> b
   1   2   3  10  10  30 100 200 300``````

## Binding two matrices

If the matrices have some identical size, then it is possible to bind them by function cbind()

``````
> M <- matrix(c(1,2,3,10,10,30), nrow=3, ncol=2, byrow=F)
> N <- matrix(c(100, 200, 300), nrow=3, ncol=1)
> B <- cbind(M, N)
> B
[,1] [,2] [,3]
[1,]    1   10  100
[2,]    2   10  200
[3,]    3   30  300``````

## Diagonal matrix

Matrix with non-zero elements located diagonally, called diagonal matrix. It is possible to create this matrix by function diag()

``````
> d <-diag(c(2,4,6))
> d
[,1] [,2] [,3]
[1,]    2    0    0
[2,]    0    4    0
[3,]    0    0    6``````

### Identity matrix

If you only specify the size of diagonal matrix, then it will create identity matrix

``````
> i <- diag(3)
> i
[,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0
[3,]    0    0    1``````

## Basic matrix operations

R give very wide selection of tools to work with matrices. Lets check some of them:

• det() - Determinant of the matrix
• qr() - QR decomposition
• svd() - Returns named list with singular value, and canonical u and v matrices for svd decomposition
• eigen() - Returns eigen values and eigen vectors of our matrix
• solve() - Returns inverse matrix

``````
> M <- matrix(c(1,3,4,-1,0,5,4,11,34), nrow=3, ncol=3, byrow=F)
> det(M)
 63
> qr(M)
\$qr
[,1]      [,2]       [,3]
[1,] -5.0990195 -3.726207 -33.928091
[2,]  0.5883484  3.480716  11.370339
[3,]  0.7844645 -0.922809  -3.549648

\$rank
 3

\$qraux
 1.196116 1.385258 3.549648

\$pivot
 1 2 3

attr(,"class")
 "qr"
> svd(M)
\$d
 36.5715204  2.6657656  0.6462128

\$u
[,1]       [,2]        [,3]
[1,] -0.1076815 -0.5574869 -0.82317254
[2,] -0.3065248 -0.7690334  0.56091901
[3,] -0.9457522  0.3127234 -0.08807287

\$v
[,1]        [,2]       [,3]
[1,] -0.1315303 -0.60533960  0.7850247
[2,] -0.1263573  0.79568289  0.5923872
[3,] -0.9832261 -0.02127673 -0.1811455

> eigen(M)
\$values
 35.993251+0.000000i -0.496625+1.226251i -0.496625-1.226251i

\$vectors
[,1]                  [,2]                  [,3]
[1,] 0.09997148+0i  0.4337928+0.5357119i  0.4337928-0.5357119i
[2,] 0.29841589+0i  0.7055059+0.0000000i  0.7055059+0.0000000i
[3,] 0.94918579+0i -0.1501591-0.0674553i -0.1501591+0.0674553i

> solve(M)
[,1]       [,2]        [,3]
[1,] -0.8730159  0.8571429 -0.17460317
[2,] -0.9206349  0.2857143  0.01587302
[3,]  0.2380952 -0.1428571  0.04761905``````

###Matrix multiplications

There are two different typo of matrix multiplications in R:

• * - element by element multiplication
• %*% - Proper full matrix multiplication

``````
> N <-diag(3)
> M*N
[,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    0    0
[3,]    0    0   34
> M%*%N
[,1] [,2] [,3]
[1,]    1   -1    4
[2,]    3    0   11
[3,]    4    5   34``````

Published: 2021-11-10 12:21:05

### 9 popular artitles

© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit. 