Author : Gianfranco David Chamorro Rodriguez
email : gianfranco.chamorror@gmail.com
Chanel : Video explicación
Matrices are a two-dimensional arrangement of numbers, a certain number of rows and a certain number of columns. Matrices are used for multiple applications and generally represent the coefficients of systems of linear equations or to represent linear applications; in this case matrices play the same role as vector data for linear maps. They can be added, multiplied, and broken down in various ways, which also makes them a key concept in field of linear algebra.
\[\begin{equation*} A_{(row , column)} = A_{(n , k)} \begin{pmatrix} a_{11} & a_{12} & \dots & a_{1k} \\ a_{21} & a_{22} & \dots & a_{2k} \\ \vdots & \vdots & \ddots & \vdots\\ a_{n1} & a_{n2} & \dots & a_{nk} \end{pmatrix} \end{equation*}\]
x <-c(2,3,5,-5,3,3,-9,-2,0,8,-2,3)
M <-matrix(x,nrow=4,ncol=3)
M
## [,1] [,2] [,3]
## [1,] 2 3 0
## [2,] 3 3 8
## [3,] 5 -9 -2
## [4,] -5 -2 3
#Creating from vectors:
age <- c(28,25,32,27,36,23)
height <- c(170,175,180,165,169,175)
weight <- c(75,80,95,80,75,68)
Z <-matrix(c(age,height,weight), nrow=6,ncol=3)
Z
## [,1] [,2] [,3]
## [1,] 28 170 75
## [2,] 25 175 80
## [3,] 32 180 95
## [4,] 27 165 80
## [5,] 36 169 75
## [6,] 23 175 68
#We can add the names or labels of rows or columns
colnames(Z) <- c('age','height','weight')
rownames(Z) <- c('John','Peter','Andrea','Melissa','Sara','Julio')
Z
## age height weight
## John 28 170 75
## Peter 25 175 80
## Andrea 32 180 95
## Melissa 27 165 80
## Sara 36 169 75
## Julio 23 175 68
A square matrix is any matrix that has the same number of rows and columns. \[\begin{equation*} A_{(3,3)} = \begin{pmatrix} 7\hspace{0.5cm}6\hspace{0.5cm}0\\ 5\hspace{0.5cm}3\hspace{0.5cm}1\\ 2\hspace{0.5cm}2\hspace{0.5cm}1\\ \end{pmatrix} \end{equation*}\]
x <-c(7,5,2,6,3,2,0,1,1)
A <-matrix(x,nrow=3,ncol=3)
A
## [,1] [,2] [,3]
## [1,] 7 6 0
## [2,] 5 3 1
## [3,] 2 2 1
A symmetric matrix is a matrix of order n with the same number of rows and columns where its transposed matrix is equal to the original matrix. \[ a_{(i,k)}=a_{(k,i)} \]
\[\begin{equation*} A_{(3,3)} = \begin{pmatrix} a_{11} \hspace{0.2cm} a_{12}\hspace{0.2cm} a_{13} \\ a_{21} \hspace{0.2cm}a_{22}\hspace{0.2cm} a_{23} \\ a_{31} \hspace{0.2cm}a_{32}\hspace{0.2cm} a_{33} \\ \end{pmatrix} \hspace{2.5cm} B_{(3,3)} = \begin{pmatrix} 5\hspace{0.2cm} 7\hspace{0.2cm} 10 \\ 7 \hspace{0.2cm}9 \hspace{0.2cm}8 \\ 10\hspace{0.2cm} 8 \hspace{0.2cm}15 \\ \end{pmatrix} \end{equation*}\]
x <-c(1,2,7,2,8,1,7,1,9)
A <-matrix(x,nrow=3,ncol=3)
A
## [,1] [,2] [,3]
## [1,] 1 2 7
## [2,] 2 8 1
## [3,] 7 1 9
In linear algebra, a diagonal matrix is a matrix in which the entries outside the main diagonal are all zero; the term usually refers to square matrices. Elements of the main diagonal can either be zero or nonzero.
\[\begin{equation*} \Large{D_{(3,3)}=}\begin{bmatrix} 7 \hspace{0.2cm} 0\hspace{0.2cm} 0 \\ 0 \hspace{0.2cm}6\hspace{0.2cm} 0 \\ 0 \hspace{0.2cm}0\hspace{0.2cm} 8 \\ \end{bmatrix} \end{equation*}\]
C1 <- c(7,0,0)
C2 <- c(0,6,0)
C3 <- c(0,0,8)
D <-matrix(c(C1,C2,C3), nrow=3,ncol=3)
D
## [,1] [,2] [,3]
## [1,] 7 0 0
## [2,] 0 6 0
## [3,] 0 0 8
The scalar matrix is a square matrix having a constant value for all the elements of the principal diagonal, and the other elements of the matrix are zero. The scalar matrix is obtained by the product of the identity matrix with a numeric constant value.
\[\begin{equation*} \Large{E_{(3,3)}=}\begin{bmatrix} 7 \hspace{0.2cm} 0\hspace{0.2cm} 0 \\ 0 \hspace{0.2cm}7\hspace{0.2cm} 0 \\ 0 \hspace{0.2cm}0\hspace{0.2cm} 7 \\ \end{bmatrix} \end{equation*}\]
C1 <- c(7,0,0)
C2 <- c(0,7,0)
C3 <- c(0,0,7)
E <-matrix(c(C1,C2,C3), nrow=3,ncol=3)
E
## [,1] [,2] [,3]
## [1,] 7 0 0
## [2,] 0 7 0
## [3,] 0 0 7
An identity matrix is a given square matrix of any order which contains on its main diagonal elements with value of one, while the rest of the matrix elements are equal to zero.
\[\begin{equation*} \Large{I_{(3)}=}\begin{bmatrix} 1 \hspace{0.2cm} 0\hspace{0.2cm} 0 \\ 0 \hspace{0.2cm}1\hspace{0.2cm} 0 \\ 0 \hspace{0.2cm}0\hspace{0.2cm} 1 \\ \end{bmatrix} \end{equation*}\]
x <-c(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)
I <-matrix(x,nrow=4,ncol=4)
I
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 0
## [2,] 0 1 0 0
## [3,] 0 0 1 0
## [4,] 0 0 0 1
A triangular matrix is a special type of square matrix where all the values above or below the diagonal are zero. L is called a lower triangular matrix and U is called an upper triangular matrix. Matrix equations of above form can be easily solved using backward substitution or forward substitution.
\[\begin{equation*} \Large{P_{(3,3)}=}\begin{bmatrix} 2 \hspace{0.2cm} \color{red}0\hspace{0.2cm} \color{red}0 \\ 8 \hspace{0.2cm}7\hspace{0.2cm} \color{red}0 \\ 6 \hspace{0.2cm}9\hspace{0.2cm} 7 \end{bmatrix} \quad \text{(Lower Triangular Matrix)} \end{equation*}\]\
\[\begin{equation*} \Large{Q_{(3,3)}=}\begin{bmatrix} 4 \hspace{0.2cm} 1\hspace{0.2cm} 6 \\ \color{red}0 \hspace{0.2cm} 5 \hspace{0.2cm} 8\\ \color{red}0 \hspace{0.2cm} \color{red}0 \hspace{0.2cm} 9 \\ \end{bmatrix} \quad \text{(Upper Triangular Matrix)} \end{equation*}\]
For two matrices to be equal, they must have the same number of columns and rows, since each element must be equal. Let matrix A equal matrix B if: \(a_{(i,k)}=b_{(i,k)}\) \[\begin{equation*} A_{(2,2)}= \begin{bmatrix} 5\hspace{0.5cm} 7\\ 7 \hspace{0.5cm}9 \\ \end{bmatrix}\hspace{2cm} = \hspace{2cm} B_{(2,2)}=\begin{bmatrix} 5\hspace{0.5cm} 7\\ 7 \hspace{0.5cm}9 \\ \end{bmatrix} \end{equation*}\]
The transpose of a matrix is simply a flipped version of the original matrix. We can transpose a matrix by switching its rows with its columns. \(a_{(i,k)}\) = \(a'_{(k,i)}\).
\[\begin{equation*} A_{(2,3)} \hspace{2cm} ; \hspace{2cm} A'_{(3,2)} \end{equation*}\]
\[\begin{equation*} \Large{A_{(2,3)}=} \begin{bmatrix} \color{red}a_{11} \hspace{0.2cm} \color{red}b_{12}\hspace{0.2cm} \color{red}c_{13} \\ \color{blue}d_{21} \hspace{0.2cm}\color{blue}e_{22}\hspace{0.2cm} \color{blue}f_{23} \end{bmatrix} \Large{A'_{(3,2)}=}\begin{bmatrix} \color{red}a_{11} \hspace{0.2cm} \color{blue}d_{12}\\ \color{red}b_{21} \hspace{0.2cm} \color{blue}e_{22}\\ \color{red}c_{31}\hspace{0.2cm} \color{blue}f_{32} \end{bmatrix} \end{equation*}\]
a <-c(2,5,8,6,2,3)
A <-matrix(a,nrow=2,ncol=3)
A
## [,1] [,2] [,3]
## [1,] 2 8 2
## [2,] 5 6 3
t(A)
## [,1] [,2]
## [1,] 2 5
## [2,] 8 6
## [3,] 2 3
To be able to add or subtract matrices, they must have the same number of rows and columns. This is so since, for both addition and subtraction, the terms that occupy the same place in the matrices are added or subtracted.
\[\begin{equation*} \Large{A_{(i,k)} + B_{(i,k)} =C_{(i,k)}}\\ \Large{A_{(i,k)} - B_{(i,k)} =D_{(i,k)}}\\ \end{equation*}\]
a <-c(1,2,5,8,8,3)
A <-matrix(a,nrow=2,ncol=3)
b <-c(3,2,2,5,1,0)
B <-matrix(b,nrow=2,ncol=3)
A
## [,1] [,2] [,3]
## [1,] 1 5 8
## [2,] 2 8 3
B
## [,1] [,2] [,3]
## [1,] 3 2 1
## [2,] 2 5 0
A+B
## [,1] [,2] [,3]
## [1,] 4 7 9
## [2,] 4 13 3
A-B
## [,1] [,2] [,3]
## [1,] -2 3 7
## [2,] 0 3 3
The condition for multiplying matrices is that the first one must have the same number of columns as the second rows. The matrix resulting from the product will have the same number of rows as the first and the same number of columns as the second. \[ A_{({\color{blue}i},\color{red}j)}*B_{({\color{red}j},{\color{blue}n})}=C_{({\color{blue}i,n})} \] \[\begin{equation*} \Large{A_{({\color{red}2},{\color{blue}3})}=}\begin{bmatrix}\ \color{magenta}4 \hspace{0.2cm} \color{magenta}5\hspace{0.2cm} \color{magenta}2 \\ \color{purple}2 \hspace{0.2cm}\color{purple}3\hspace{0.2cm} \color{purple}1 \\ \end{bmatrix} \hspace{3cm} \Large{B_{{(\color{blue}3},{\color{red}2})}=} \begin{bmatrix} \color{green}2 \hspace{0.2cm} \color{brown}4\\ \color{green}7 \hspace{0.2cm} \color{brown}1\\ \color{green}3\hspace{0.2cm} \color{brown}5 \end{bmatrix} \end{equation*}\]\end{equation*} \[\begin{equation*} \Large{AB_{{\color{blue}(2,2)}}=} \begin{bmatrix} {\color{magenta}4} *{\color{green}2} + {\color{magenta}5} * {\color{green}7} + {\color{magenta}2} * {\color{green}3} _{(1,1)} \hspace{2cm} {\color{magenta}4} *{\color{brown}4} + {\color{magenta}5} * {\color{brown}1} + {\color{magenta}2} *{\color{brown}5} _{(1,2)} \\ {\color{purple}2} *{\color{green}2} + {\color{purple}3} * {\color{green}7} + {\color{purple}1} * {\color{green}3}_{(2,1)} \hspace{2cm} {\color{purple}2} *{\color{brown}4} + {\color{purple}3} * {\color{brown}1} + {\color{purple}1} * {\color{brown}5} _{(2,2)} \end{bmatrix} \end{equation*}\] \[\begin{equation*} {AB_{{\color{blue}(2,2)}}=}\begin{bmatrix} 49 \hspace{2cm} 31\\ 28\hspace{2cm}16 \end{bmatrix} \end{equation*}\]
a <-c(4,2,5,3,2,1)
A <-matrix(a,nrow=2,ncol=3)
b <-c(2,7,3,4,1,5)
B <-matrix(b,nrow=3,ncol=2)
A%*%B
## [,1] [,2]
## [1,] 49 31
## [2,] 28 16
let Y a vector \(nx1\) \[\begin{equation*} \Large{Y_{(n,1)}=} \begin{bmatrix} Y_{1,1}\\ Y_{2,1} \\ \vdots\\ Y_{n,1} \end{bmatrix} \Large{Y'_{(1,n)}=}\begin{bmatrix} Y_{1,1} & Y_{1,2} & \dots & Y_{1,n} \end{bmatrix} \end{equation*}\]
\[\begin{equation*} Y'_{({\color{red}1},n)} Y_{(n,{\color{red}1})} = (Y_{1,1}'*Y_{1,1}) + (Y_{1,2}'*Y_{2,1}) + \dots + (Y_{n,1}'*Y_{1,n}) \\ Y'Y_{({\color{red}1,\color{red}1})}= Y_{1}^2 + Y_{2}^2 + \dots + Y_{n}^2 \\ Y'Y_{({\color{red}1,\color{red}1})}=\sum^{n}_{{i=1}} Y_i^2 \end{equation*}\]
y <-c(5,3,4,9,6,2,2)
Y <- cbind(y)
Y
## y
## [1,] 5
## [2,] 3
## [3,] 4
## [4,] 9
## [5,] 6
## [6,] 2
## [7,] 2
t(Y)%*%Y
## y
## y 175
\[\begin{equation*} \LARGE{Y_{({\color{red}n},1)}}Y'_{(1,{\color{red}n})} = YY'_{({\color{red}n,\color{red}n})} = \begin{bmatrix} {\color{red}Y_{1}^2} \hspace{1cm} Y_{1}Y_{2} \hspace{1cm} \dots \hspace{1cm} Y_{1}Y_{n} \\ Y_{2}Y_{1} \hspace{1cm} {\color{red}Y_{2}^2} \hspace{1cm} \dots \hspace{1cm} Y_{2}Y_{n} \\ \vdots \hspace{2.5cm} \vdots \hspace{1.5cm} \ddots \hspace{1.8cm} \vdots\\ Y_{n}Y_{1} \hspace{1cm} Y_{n}Y_{2} \hspace{1cm} \dots \hspace{1cm} {\color{red}Y_{n}^2} \end{bmatrix} \end{equation*}\]
y <-c(5,3,4,9,6,2,2)
Y <- cbind(y)
Y
## y
## [1,] 5
## [2,] 3
## [3,] 4
## [4,] 9
## [5,] 6
## [6,] 2
## [7,] 2
Y%*%t(Y)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 25 15 20 45 30 10 10
## [2,] 15 9 12 27 18 6 6
## [3,] 20 12 16 36 24 8 8
## [4,] 45 27 36 81 54 18 18
## [5,] 30 18 24 54 36 12 12
## [6,] 10 6 8 18 12 4 4
## [7,] 10 6 8 18 12 4 4
let the vector \(i\):
\[\begin{equation*} \Large{i_{(n,1)}=} \begin{bmatrix} 1 \\ 1 \\ \vdots\\ 1 \end{bmatrix} \hspace{0.5cm} \LARGE{i'_{(1 , n)}} = \begin{bmatrix} 1 & 1 & \dots 1 \end{bmatrix} \end{equation*}\]
To find the sum of elements of the column vector Y:
\[\begin{equation*} \LARGE{Y_{(n , 1)}} = \begin{bmatrix} y_{1} \\ y_{2}\\ \vdots\\ y_{n} \end{bmatrix} \end{equation*}\]
\[\begin{equation*} \Large{i'_{({\color{red}1},n)} Y_{(n,{\color{red}1})} = (y_{1}*1) + (y_{2}*1) + \dots + (y_{n}*1) =\sum^{n}_{{i=1}} Y_i } \end{equation*}\]
y<- c(2,3,5,8,4,3,2,1)
i<- matrix(1,nrow=8,ncol=1)
Y<- cbind(y)
Y
## y
## [1,] 2
## [2,] 3
## [3,] 5
## [4,] 8
## [5,] 4
## [6,] 3
## [7,] 2
## [8,] 1
i
## [,1]
## [1,] 1
## [2,] 1
## [3,] 1
## [4,] 1
## [5,] 1
## [6,] 1
## [7,] 1
## [8,] 1
t(i)%*%Y
## y
## [1,] 28
#Another way to find the sum of elements of a vector:
sum(Y)
## [1] 28
Deviations from the Mean:
Remember \(\bar{x}\):
\[\begin{equation*} i\bar{x}= i \frac{1}{n}i'x \end{equation*}\]
\[\begin{equation*} \begin{bmatrix} \bar{x}\\ \bar{x}\\ \vdots \\ \bar{x} \end{bmatrix} = \underbrace{{\frac{1}{n}} \;i\;i'}_{MATRIZ_{(n,n)}}\;x \end{equation*}\]
\[\begin{equation*} [x-\bar{x}] = [x- {\frac{1}{n}}ii'x] \end{equation*}\]
Remember \([X]=I[X]\)
\[\begin{equation*} [IX - {\frac{1}{n}}ii'X] \end{equation*}\]
\[\begin{equation*} [I - {\frac{1}{n}}ii']X \\ M^ºX \end{equation*}\] The idempotent Matrix has as diagonal \(1-1/n\) and off the diagonal \(-1/n\)
i <-matrix(1,nrow=3,ncol=1)
Identity <-c(1,0,0,0,1,0,0,0,1)
I <-matrix(Identity,nrow=3,ncol=3)
iit= i%*%t(i) / 3
Mº= (I-iit)
Mº # Idempotent Matrix
## [,1] [,2] [,3]
## [1,] 0.6666667 -0.3333333 -0.3333333
## [2,] -0.3333333 0.6666667 -0.3333333
## [3,] -0.3333333 -0.3333333 0.6666667
i # Matrix of ones
## [,1]
## [1,] 1
## [2,] 1
## [3,] 1
I # Identity Matrix
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
Properties:
Mº is symmetric.
MºMº = Mº
Mº’Mº = Mº
For a variable X the sum of squares of deviations is: \[\begin{equation*} \Large{ \sum (X_{i}-\bar{X})^2 = \sum (X_{i}^2 - 2 \bar{X}X_{i}+\bar{X}^2) = (\sum X_{i}^2) - n\bar{X}^2 } \end{equation*}\]
In matrix terms: \[\begin{equation*} \sum (X_{i}-\bar{X})^2 = (X_{i}-\bar{X}) ' (X_{i}-\bar{X}) \\ \end{equation*}\] \[\begin{equation*} (M^{\circ}X)'(M^{\circ}X) \end{equation*}\]
\[\begin{equation*} X'Mº'MºX \end{equation*}\]
\[\begin{equation*} X' M^{\circ}X \end{equation*}\]
x<-c(8,4,6)
X<- cbind(x)
XtMX = t(X)%*%Mº%*%X
XtMX
## x
## x 8
We can build a Matrix of Sum of Squares and Cross Products of deviations from the means for two vectors \(X\) and \(Y\).
\[\begin{equation*} \sum(X_{i}-\bar{X}) (Y_{i}-\bar{Y})=(M^{\circ}X)'(M^{\circ}Y) \end {equation*}\]
\[\begin{bmatrix} \sum(X_{i}-\bar{X})^2 \hspace{2cm} \sum(X_{i}-\bar{X})(Y_{i}-\bar{Y})\\ \sum(Y_{i}-\bar{Y})(X_{i}-\bar{X})\hspace{2cm} \sum(Y_{i}-\bar{Y})^2 \end{bmatrix}\] \[\begin{bmatrix} X'M^{\circ}X \hspace{2cm} X'M^{\circ}Y\\ Y'M^{\circ}X\hspace{2cm} Y'M^{\circ}Y \end{bmatrix}\]y <-c(2,3,4)
Y <-cbind(y)
a11 <- t(X)%*%Mº%*%X
a21 <- t(Y)%*%Mº%*%X
a12 <- t(X)%*%Mº%*%Y
a22 <- t(Y)%*%Mº%*%Y
spc <- c(a11,a21,a12,a22)
SPC <- matrix(spc,nrow = 2, ncol = 2 )
SPC
## [,1] [,2]
## [1,] 8 -2
## [2,] -2 2
The use of determinants greatly simplifies the resolution of systems of linear equations. To do this, general properties are applied that allow the discussion and resolution of such systems to be undertaken through a rigorous procedure. It is a matrix inversibility test, for which we work with square matrices.
a <- c(1,2,8,6,1,5,2,3,8,4,9,5,2,0,7,4)
A <- matrix(a,nrow = 4, ncol=4)
A
## [,1] [,2] [,3] [,4]
## [1,] 1 1 8 2
## [2,] 2 5 4 0
## [3,] 8 2 9 7
## [4,] 6 3 5 4
det(A) # Determinant of Matrix A
## [1] 39
solve(A) # Inverse de of Matrix A
## [,1] [,2] [,3] [,4]
## [1,] 1.4615385 -1.8205128 -3.0256410 4.564103
## [2,] -1.0769231 1.3589744 1.9487179 -2.871795
## [3,] 0.6153846 -0.5384615 -0.9230769 1.307692
## [4,] -2.1538462 2.3846154 4.2307692 -6.076923
# Resume and other functions
#We can create a matrix through the union of column vectors
a1 <- c(3,2,5,8,6)
a2 <- c(8,7,3,9,3)
a3 <- c(3,4,1,6,6)
a4 <- c(9,3,1,4,0)
a5 <- c(1,2,0,8,2)
A <-cbind(a1,a2,a3,a4,a5)
A
## a1 a2 a3 a4 a5
## [1,] 3 8 3 9 1
## [2,] 2 7 4 3 2
## [3,] 5 3 1 1 0
## [4,] 8 9 6 4 8
## [5,] 6 3 6 0 2
#We can create a matrix through the union of row vectors
b1 <- c(0,1,0,1,3)
b2 <- c(1,7,2,2,1)
b3 <- c(2,9,3,5,4)
b4 <- c(9,5,3,4,7)
b5 <- c(6,2,7,6,8)
B <-rbind(b1,b2,b3,b4,b5)
B
## [,1] [,2] [,3] [,4] [,5]
## b1 0 1 0 1 3
## b2 1 7 2 2 1
## b3 2 9 3 5 4
## b4 9 5 3 4 7
## b5 6 2 7 6 8
dim(A) # Matrix A Dimension
## [1] 5 5
nrow(A) # Number of rows in Matrix A
## [1] 5
ncol(A) # Number of columns in Matrix A
## [1] 5
t(A) # Transpose of Matrix A
## [,1] [,2] [,3] [,4] [,5]
## a1 3 2 5 8 6
## a2 8 7 3 9 3
## a3 3 4 1 6 6
## a4 9 3 1 4 0
## a5 1 2 0 8 2
solve(A) # Inverse of Matrix A
## [,1] [,2] [,3] [,4] [,5]
## a1 0.01483680 -0.13395507 0.1708351 0.02437474 0.02903773
## a2 -0.10089021 0.28232302 0.1526070 -0.02289106 -0.14031369
## a3 0.04451039 0.02670623 -0.2017804 -0.06973294 0.22997033
## a4 0.18397626 -0.20389996 -0.1102162 0.01653243 0.04578211
## a5 -0.02670623 -0.10173802 -0.1360746 0.17041119 -0.06655362
det(A) # Determinant of Matrix A
## [1] -4718
qr(A) $rank # Matrix A Rank
## [1] 5
eigen(A) # Eigenvalues and Eigenvectors of Matrix A
## eigen() decomposition
## $values
## [1] 20.689373+0.00000i -1.421612+5.02616i -1.421612-5.02616i -3.344927+0.00000i
## [5] 2.498778+0.00000i
##
## $vectors
## [,1] [,2] [,3] [,4]
## [1,] 0.5470483+0i 0.4984887-0.1445515i 0.4984887+0.1445515i -0.52637480+0i
## [2,] 0.3359223+0i 0.0602646+0.1409489i 0.0602646-0.1409489i -0.27325187+0i
## [3,] 0.2240521+0i -0.1906021-0.4224541i -0.1906021+0.4224541i 0.70602430+0i
## [4,] 0.6684371+0i -0.0883510+0.3649339i -0.0883510-0.3649339i 0.38400538+0i
## [5,] 0.3014745+0i -0.5927362+0.0000000i -0.5927362+0.0000000i -0.04829652+0i
## [,5]
## [1,] -0.32435065+0i
## [2,] 0.69826988+0i
## [3,] -0.05134298+0i
## [4,] -0.55000813+0i
## [5,] -0.31948518+0i
diag(A) # Diagonal of Matrix A
## [1] 3 7 1 4 2
sum(diag(A)) # Trace of Matrix A
## [1] 17
rowSums(A) # Sum of rows of Matrix A
## [1] 24 18 10 35 17
colSums(A) # Sum of columns of Matrix A
## a1 a2 a3 a4 a5
## 24 30 20 17 13
rowMeans(A) # Mean of the rows of Matrix A
## [1] 4.8 3.6 2.0 7.0 3.4
colMeans(A) # Mean of the columns of Matrix A
## a1 a2 a3 a4 a5
## 4.8 6.0 4.0 3.4 2.6
diag(1,3,3) # Identity Matrix 3x3
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
A%*%B # Matrix Product
## [,1] [,2] [,3] [,4] [,5]
## [1,] 101 133 59 76 100
## [2,] 54 106 49 60 66
## [3,] 14 40 12 20 29
## [4,] 105 161 104 120 149
## [5,] 27 85 38 54 61
rbind(A,B) # concatenate by rows of matrices
## a1 a2 a3 a4 a5
## 3 8 3 9 1
## 2 7 4 3 2
## 5 3 1 1 0
## 8 9 6 4 8
## 6 3 6 0 2
## b1 0 1 0 1 3
## b2 1 7 2 2 1
## b3 2 9 3 5 4
## b4 9 5 3 4 7
## b5 6 2 7 6 8
cbind(A,B) # concatenate by columns of matrices
## a1 a2 a3 a4 a5
## b1 3 8 3 9 1 0 1 0 1 3
## b2 2 7 4 3 2 1 7 2 2 1
## b3 5 3 1 1 0 2 9 3 5 4
## b4 8 9 6 4 8 9 5 3 4 7
## b5 6 3 6 0 2 6 2 7 6 8