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
Transpose Matrix In R
R中的转置矩阵

R中矩阵的对角线 (Diagonals of a Matrix in R)

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 matricesStackExchange –两个矩阵的外积
  • StackOverflow – What is R’s crossproduct function?StackOverflow – R的叉积函数是什么?

翻译自: https://www.journaldev.com/35454/matrix-operations-in-r

r语言矩阵运算

r语言矩阵运算_R中的矩阵运算相关推荐

  1. r语言正则表达式_R中的正则表达式

    r语言正则表达式 Regular expressions in R or a regex are a sequence of special characters that are defined t ...

  2. r语言在java中的实现_R语言在现实中的应用

    R语言在现实中的应用有哪些?主要有以下几种 - 1.数据科学 "哈佛商业评论"将数据科学家命名为"21世纪最性感的工作". Glassdoor将其命名为2016 ...

  3. R语言可视化图像中最常用的点样式(pch、plot characters)列表、ggpubr::show_point_shapes可视化最常用的点样式(pch)

    R语言可视化图像中最常用的点样式(pch.plot characters)列表.ggpubr::show_point_shapes可视化最常用的点样式(pch) 目录

  4. R语言e1071包中的支持向量机:构建nu-classification类型的支持向量机SVM并分析不同nu值惩罚下模型分类螺旋线型(sprials)线性不可分数据集的表现

    R语言e1071包中的支持向量机:构建nu-classification类型的支持向量机SVM并分析不同nu值惩罚下模型分类螺旋线型(sprials)线性不可分数据集的表现 目录

  5. R语言e1071包中的支持向量机:仿真数据(螺旋线性不可分数据集)、简单线性核的支持向量机SVM(模型在测试集上的表现、可视化模型预测的结果、添加超平面区域与原始数据标签进行对比分析)、如何改进核函数

    R语言e1071包中的支持向量机:仿真数据(螺旋线性不可分数据集).简单线性核的支持向量机SVM(模型在测试集上的表现.可视化模型预测的结果.添加超平面区域与原始数据标签进行对比分析).如何改进核函数 ...

  6. R语言在ggplot中使用变量指定柱状图的名称实战

    R语言在ggplot中使用变量指定柱状图的名称实战 目录 R语言在ggplot中使用变量指定柱状图的名称实战

  7. R语言把dataframe中的NA值替换为0

    R语言把dataframe中的NA值替换为0 目录 R语言把dataframe中的NA值替换为0 缺失值替换 方法总结 缺失值替换

  8. R语言在金融中的应用二

    3.文件输入输出以及常见错误 3.1 R脚本文件输入(打开) .R 利用Rstudio界面 利用R界面 命令打开 file.edit("#dir",fileEncoding = & ...

  9. R语言在金融中的运用一

    R语言在金融中的运用 财富管理 风控 数据采集 新浪微博.新浪新闻.股吧.Google.Bloomberg.新浪博客.人民日报.雪球.twitter.Seeking Alpha 继承S语言 1.R包相 ...

最新文章

  1. java代码_阿里资深工程师教你如何优化 Java 代码!
  2. Python代码运行助手
  3. python 可变参数 关键字参数_Python之 可变参数和关键字参数
  4. 目录同步 linux,Linux系统目录实时同步
  5. android mvc mvp 简书,浅析 MVP,MVC,MVVM模式(Android)
  6. 面试官问你有什么要问的时候,大胆的提出类似问题
  7. (一)Linux下C++ OpenCV开发环境搭建
  8. mcrotime php_php时间函数time、date和microtime的区别 | 木凡博客
  9. 打造IE6的position:fixed整理篇
  10. [转]android刷新后R.java不见了
  11. 解决Required String parameter xxx is not present异常
  12. 还原mysql数据库出错_mysql数据库还原出错ERROR:Unknown command ‘\\’解决手记
  13. REST Assured 55 - JSON Schema Validation In Rest Assured
  14. 基于python及图像识别的围棋棋盘棋子识别3——耗时优化(一行代码速度提高600倍)
  15. 教你区别几款常用的U盘修复工具
  16. uniapp app端根据支付宝qr链接 直接打开支付宝app支付页面
  17. 荣品RK3288开发板 怎么接23.5寸,BOA屏?
  18. Quasi_Binary(模拟)
  19. 最新目标跟踪评估指标汇总
  20. 纯CSS 毛玻璃效果

热门文章

  1. BizTalk Server 2010新功能介绍(四):基于安全连接的FTP适配器
  2. [转载] python隐式转换_Python | 数据类型的转换 显式转换 隐式转换
  3. DT大数据 scala for查询
  4. PATB1017 A除以B
  5. PyTorch 入坑十一: 损失函数、正则化----深刻剖析softmax+CrossEntropyLoss
  6. 更换系统后无法catkin_make universal robot 包
  7. C++ Primer Plus学习(八)——函数进阶
  8. LeetCode-21.合并两个有序链表(链表+递归)
  9. 网优5g前景_网优行业的吐槽,附吐槽记录
  10. linux修改jdk文件保存,Linux下更换jdk和配置环境变量详解