r语言矩阵运算
R offers extensive matrix handling capabilities. In addition to the basic operations discussed earlier, there are several advanced matrix functions that will ease your statistical programming efforts. This tutorial will illustrate such functions with examples.
R提供了广泛的矩阵处理功能。 除了前面讨论的基本操作外,还有一些高级矩阵函数可简化统计编程工作。 本教程将通过示例说明此类功能。
R中的矩阵转置 (Transpose of Matrix in R)The transpose of a matrix A is simply another matrix with the rows and columns interchanged. This can be calculated using t(A) in R.
矩阵A的转置只是行和列互换的另一个矩阵。 可以使用R中的t(A)来计算。
#Define a rectangular matrix
> rectmatrix<-matrix(c(2,3,4,5,6,7),nrow=2,ncol=3)
> rectmatrix
[,1] [,2] [,3]
[1,] 2 4 6
[2,] 3 5 7
> rectmatrix
[,1] [,2] [,3]
[1,] 2 4 6
[2,] 3 5 7
> t(rectmatrix)
[,1] [,2]
[1,] 2 3
[2,] 4 5
[3,] 6 7
R has the diag() function to create a diagonal matrix from a vector. The same function can also be used to retrieve the elements along the principal diagonal of a matrix. Even for non-square matrices, the diag() function returns the diagonal starting from elements [1,1], [2,2] …etc till no such elements can be retrieved.
R具有diag()函数,可根据矢量创建对角矩阵。 相同的函数也可以用于检索沿矩阵主对角线的元素。 即使对于非平方矩阵, diag()函数也会从元素[1,1],[2,2]等开始返回对角线,直到无法检索到此类元素。
#diag(x) where x is a scalar - returns an identity matrix of dimension x by #x
> diagmatrix<-diag(4)
> diagmatrix
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 1 0 0
[3,] 0 0 1 0
[4,] 0 0 0 1
#diag(x,y,z) - returns a matrix of size y by z with element x along its #diagonal
> diagmatrix1<-diag(4,2,2)
> diagmatrix1
[,1] [,2]
[1,] 4 0
[2,] 0 4
#Define a rectangular matrix
> rectmatrix<-matrix(c(2,3,4,5,6,7),nrow=2,ncol=3)
> rectmatrix
[,1] [,2] [,3]
[1,] 2 4 6
[2,] 3 5 7
#Extract its diagonal - returns [1,1] and [2,2] elements of rectmatrix
> diag(rectmatrix)
[1] 2 5
R中的跨产品和外部产品矩阵运算 (Cross product and Outer product Matrix Operations in R)
We have already seen element-wise multiplication and matrix multiplication earlier. Matrices also have two other kinds of products that are supported by R.
前面我们已经看到了逐元素乘法和矩阵乘法 。 矩阵还具有R支持的其他两种产品。
- Outer product: In the simplest terms, the outer product is defined over two vectors v1 and v2, resulting in a matrix that consists of every element of v1 multiplied by every element of v2. If v1 is of length m and v2 is of length n, the outer product is a matrix of dimension m by n. This is also known as the tensor product sometimes. This notion can also be generalized to matrices and known as the Kroeckner product in that case. The R operator for this is %o%.
外部乘积:用最简单的术语来说,外部乘积是在两个向量v1和v2上定义的,从而形成一个矩阵,该矩阵由v1的每个元素乘以v2的每个元素组成。 如果v1的长度为m,v2的长度为n,则外部乘积是m乘n的矩阵。 有时也称为张量积 。 该概念也可以推广到矩阵,在这种情况下称为Kroeckner乘积 。 为此的R运算符是%o% 。
- Cross product: The result of a cross product for two vectors A and B is another vector C that is orthogonal to both A and B. Though the intuition behind this is not obviously evident, cross-product has several applications in mathematics, physics, and statistics. This can be done using the crossprod() function in R. 叉积:叉积的两个矢量A和B的结果是另一矢量C正交于A和B两者虽然这背后的直觉显然不是明显的,跨产品具有在数学,物理几个应用程序,并统计。 这可以使用R中的crossprod()函数来完成。
Let us look at examples to illustrate these.
让我们看一些例子来说明这些。
#Calculate the vector outerproducts using R
> vec1 <- c(2,4,6)
> vec2 <-c(5,2,2,2,8)
> op <- vec1%o%vec2
> op
[,1] [,2] [,3] [,4] [,5]
[1,] 10 4 4 4 16
[2,] 20 8 8 8 32
[3,] 30 12 12 12 48
#Get the crossproduct of a matrix with itself
> x
[,1] [,2] [,3]
[1,] 3 2 8
[2,] 4 6 9
[3,] 5 7 11
> y
[,1] [,2] [,3]
[1,] 3 7 1
[2,] 2 8 54
[3,] 6 22 10
> crossprod(x)
[,1] [,2] [,3]
[1,] 50 65 115
[2,] 65 89 147
[3,] 115 147 266
#Crossproduct of two vectors x and y
> crossprod(x,y)
[,1] [,2] [,3]
[1,] 47 163 269
[2,] 60 216 396
[3,] 108 370 604
R中的列和行矩阵运算 (Column and Row Matrix Operations in R)
R provides several handy functions for combining matrices and generating sums and means of rows and columns. These functions are listed below with examples.
R提供了一些方便的函数,用于组合矩阵并生成总和以及行和列的均值。 以下示例列出了这些功能。
cbind(): Combines two or more matrices or data frames column-wise to return a new matrix/data frame.
cbind() :按列组合两个或更多矩阵或数据帧以返回新的矩阵/数据帧。
> x
[,1] [,2] [,3]
[1,] 3 2 8
[2,] 4 6 9
[3,] 5 7 11
> y
[,1] [,2] [,3]
[1,] 3 7 1
[2,] 2 8 54
[3,] 6 22 10
> cbind(x,y)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 3 2 8 3 7 1
[2,] 4 6 9 2 8 54
[3,] 5 7 11 6 22 10
>
rbind(): Combines two more matrices or data frames row-wise similar to the above function.
rbind() :类似于上述功能,按行组合两个以上的矩阵或数据帧。
> x
[,1] [,2] [,3]
[1,] 3 2 8
[2,] 4 6 9
[3,] 5 7 11
> y
[,1] [,2] [,3]
[1,] 3 7 1
[2,] 2 8 54
[3,] 6 22 10
> rbind(x,y)
[,1] [,2] [,3]
[1,] 3 2 8
[2,] 4 6 9
[3,] 5 7 11
[4,] 3 7 1
[5,] 2 8 54
[6,] 6 22 10
colSums() and colMeans(): Generates sums and means of elements column-wise for a matrix or data frame.
colSums()和colMeans() :为矩阵或数据帧按列生成元素的总和和均值。
> x
[,1] [,2] [,3]
[1,] 3 2 8
[2,] 4 6 9
[3,] 5 7 11
> colSums(x)
[1] 12 15 28
> colMeans(x)
[1] 4.000000 5.000000 9.333333
rowSums() and rowMeans(): Generates sums and means of elements row-wise for a matrix or data frame.
rowSums()和rowMeans() :为矩阵或数据帧逐行生成总和和元素均值。
#Define x and y matrices
> x<-matrix(c(3,4,5,2,6,7,8,9,11),ncol=3,nrow=3)
> y<-matrix(c(3,2,6,7,8,22,1,54,10),ncol=3,nrow=3)
> x
[,1] [,2] [,3]
[1,] 3 2 8
[2,] 4 6 9
[3,] 5 7 11
> rowSums(x)
[1] 13 19 23
> rowMeans(x)
[1] 4.333333 6.333333 7.666667
参考文献: (References:)
- Wikipedia – Cross Product 维基百科–跨产品
- Wikipedia – Outer product 维基百科-外部产品
- StackExchange – Outerproduct of two matrices StackExchange –两个矩阵的外积
- StackOverflow – What is R’s crossproduct function? StackOverflow – R的叉积函数是什么?
翻译自: https://www.journaldev.com/35454/matrix-operations-in-r
r语言矩阵运算



