# Row i is ai column j is aj if u and v are vectors of

• Notes
• 130

This preview shows pages 14–27. Sign up to view the full content.

Row i is A[i,] , column j is A[,j]. If u and v are vectors of indices (in the correct range), then A[u,v] is the submatrix with rows in u and columns in v . 14

This preview has intentionally blurred sections. Sign up to view the full version.

> A<-diag(1:6) > A [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 0 0 0 0 0 [2,] 0 2 0 0 0 0 [3,] 0 0 3 0 0 0 [4,] 0 0 0 4 0 0 [5,] 0 0 0 0 5 0 [6,] 0 0 0 0 0 6 > A[3:1,3:4] [,1] [,2] [1,] 3 0 [2,] 0 0 [3,] 0 0 > A[6,] [1] 0 0 0 0 0 6 15
2. Basic Matrix Operations 16

This preview has intentionally blurred sections. Sign up to view the full version.

If A is a matrix with n rows and m columns, then t (A) is the transposed matrix with m rows and n columns. In matrix notation we write A' . It is clear that A= t ( t (A)) . > x<-matrix(1:6,3,2) > matrix(x,2,3,byrow=TRUE) [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 > t(x) [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 17
Addition Two matrices A and B with the same dimension attribute (the same number of rows and columns) can be added. Actually in R something more general seems to be true. If we want to add vector A to a matrix B , where length(A)<=prod(dim(B)) , then R does dim(A)<-dim(B) before doing A+B . If A is too short, its elements get reused . This still implies only matrices of the same order get added, of course. 18

This preview has intentionally blurred sections. Sign up to view the full version.

Reusing is useful for adding the same number to all elements of a matrix. > 1+matrix(1:6,3,2) [,1] [,2] [1,] 2 5 [2,] 3 6 [3,] 4 7 Or for adding a vector to all columns of a matrix. > 1:3+matrix(1:6,3,2) [,1] [,2] [1,] 2 5 [2,] 4 7 [3,] 6 9 19
Other Elementwise Operations Suppose is any binary operator such as addition , subtraction , multiplication , or division . If two matrices A and B have the same dimension, then C=A ‡ B is defined, and C has elements equal to C[i,j]=A[i,j]‡B[i,j] . If is a unary operator such as sin , log , abs , or pnorm then B=‡A is defined and B has elements B[i,j]=‡A[i,j] . 20

This preview has intentionally blurred sections. Sign up to view the full version.

Vector-vector multiplication In R the inner product of two vectors a and b is sum (a*b) . Remember, the inner product of two vectors is defined by 21 a b = n i =1 a i b i In R the inner product can also be written as x %*% x , but this is pretty ugly, because it is ambiguous (it might as well mean outer (x,x) ). Better is crossprod (x) .
If x is a vector, then crossprod (x) is i.e. the inner product is the sum of squares. Thus crossprod(x-mean(x))/length(x) is the variance and crossprod(x-mean(x),y-mean(y))/length(x) is the covariance. 22 n i =1 x 2 i

This preview has intentionally blurred sections. Sign up to view the full version.

cor<-function(x,y) { nx<-length(x); ny<-length(y) if (nx != ny) stop("not equal length") mx<-mean(x); my<-mean(y) xx<-x-mean(x); yy<-y-mean(y) cxx<-crossprod(xx); cyy<-crossprod(yy) cxy<-crossprod(xx,yy) return(list(n=nx,mx=mx,my=my, vx=cxx/(nx-1),vy=cyy/(ny-1), cxy=cxy/(nx-1), rxy=cxy/sqrt(cxx*cyy))) } 23
Generalized Outer Product outer() is a very versatile function that can be used to make matrices out of vectors. The simplest use is illustrated below. > outer(1:3,1:2) [,1] [,2] [1,] 1 2 [2,] 2 4 [3,] 3 6 Thus outer( x,y ) gives a matrix A with elements A[i,j]=x[i]*y[j]. 24

This preview has intentionally blurred sections. Sign up to view the full version.

> outer(1:3,1:3,"+") [,1] [,2] [,3] [1,] 2 3 4 [2,] 3 4 5 [3,] 4 5 6 > outer(1:3,1:3,"^") [,1] [,2] [,3] [1,] 1 1 1 [2,] 2 4 8 [3,] 3 9 27 > outer(1:3,2:3,function(x,y) x%%y) [,1] [,2] [1,] 1 1 [2,] 0 2 [3,] 1 0 25
Matrix-Vector Multiplication If A is a matrix with n rows and m columns, then A can be multiplied by a vector x with m elements using A %*% x . The result is a linear combination (weighted sum) of the columns of A , with weights given by x . Thus In the same way x %*% A makes a weighted sum of the rows (thus x must have n elements).

This preview has intentionally blurred sections. Sign up to view the full version.

This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern