<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>矩阵运算 on 格物堂</title><link>https://gewutang.cn/tags/%E7%9F%A9%E9%98%B5%E8%BF%90%E7%AE%97/</link><description>Recent content in 矩阵运算 on 格物堂</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Sat, 27 Aug 2011 00:00:00 +0000</lastBuildDate><atom:link href="https://gewutang.cn/tags/%E7%9F%A9%E9%98%B5%E8%BF%90%E7%AE%97/index.xml" rel="self" type="application/rss+xml"/><item><title>R与矩阵运算总结</title><link>https://gewutang.cn/2011/08/27/matrix-operations/</link><pubDate>Sat, 27 Aug 2011 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2011/08/27/matrix-operations/</guid><description>&lt;h1 id="1-矩阵基本操作"&gt;&lt;a href="#1-%e7%9f%a9%e9%98%b5%e5%9f%ba%e6%9c%ac%e6%93%8d%e4%bd%9c" class="header-anchor"&gt;&lt;/a&gt;1 矩阵基本操作&#10;&lt;/h1&gt;&lt;h2 id="11创建向量"&gt;&lt;a href="#11%e5%88%9b%e5%bb%ba%e5%90%91%e9%87%8f" class="header-anchor"&gt;&lt;/a&gt;1.1创建向量&#10;&lt;/h2&gt;&lt;p&gt;R里面有多种方法来创建向量（Vector），最简单的是用函数c()。例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;X=c(1,2,3,4)&#10;&#10;X&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 1 2 3 4&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;当然，还有别的方法。例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;X=1:4&#10;&#10;X&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 1 2 3 4&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;还有seq()函数。例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;X=seq(1,4,length=4)&#10;&#10;X&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 1 2 3 4&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;注意一点，R中的向量默认为列向量，如果要得到行向量需要对其进行转置。&lt;/p&gt;&#10;&lt;h2 id="12创建矩阵"&gt;&lt;a href="#12%e5%88%9b%e5%bb%ba%e7%9f%a9%e9%98%b5" class="header-anchor"&gt;&lt;/a&gt;1.2创建矩阵&#10;&lt;/h2&gt;&lt;p&gt;R中创建矩阵的方法也有很多。大致分为直接创建和由其它格式转换两种方法。&lt;/p&gt;&#10;&lt;h3 id="121直接创建矩阵"&gt;&lt;a href="#121%e7%9b%b4%e6%8e%a5%e5%88%9b%e5%bb%ba%e7%9f%a9%e9%98%b5" class="header-anchor"&gt;&lt;/a&gt;1.2.1直接创建矩阵&#10;&lt;/h3&gt;&lt;p&gt;最简单的直接创建矩阵的方法是用matrix()函数，matrix()函数的使用方法如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;args(matrix)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL) &#10;## NULL&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;其中，data参数输入的为矩阵的元素，不能为空；nrow参数输入的是矩阵的行数，默认为1；ncol参数输入的是矩阵的列数，默认为1；byrow参数控制矩阵元素的排列方式，TRUE表示按行排列，FALSE表示按列排列，默认为FALSE；dimnames参数输入矩阵的行名和列名，可以不输入，系统默认为NULL。例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;matrix(1:6,nrow=2,ncol=3,byrow=FALSE)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1 3 5&#10;## [2,] 2 4 6&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;改变矩阵的行数和列数：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;matrix(1:6,nrow=3,ncol=2,byrow=FALSE)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2]&#10;## [1,] 1 4&#10;## [2,] 2 5&#10;## [3,] 3 6&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;改变byrow参数：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;matrix(1:6,nrow=3,ncol=2,byrow=T)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2]&#10;## [1,] 1 2&#10;## [2,] 3 4&#10;## [3,] 5 6&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;设定矩阵的行名和列名：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;matrix(1:6,nrow=3,ncol=2,byrow=T,dimnames=list(c(&amp;quot;A&amp;quot;,&amp;quot;B&amp;quot;,&amp;quot;C&amp;quot;),c(&amp;quot;boy&amp;quot;,&amp;quot;girl&amp;quot;)))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## boy girl&#10;## A 1 2&#10;## B 3 4&#10;## C 5 6&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h3 id="122-由其它格式转换"&gt;&lt;a href="#122-%e7%94%b1%e5%85%b6%e5%ae%83%e6%a0%bc%e5%bc%8f%e8%bd%ac%e6%8d%a2" class="header-anchor"&gt;&lt;/a&gt;1.2.2 由其它格式转换&#10;&lt;/h3&gt;&lt;p&gt;也可以由其它格式的数据转换为矩阵，此时需要用到函数as.matrix()。&lt;/p&gt;&#10;&lt;h2 id="13-查看和改变矩阵的维数"&gt;&lt;a href="#13-%e6%9f%a5%e7%9c%8b%e5%92%8c%e6%94%b9%e5%8f%98%e7%9f%a9%e9%98%b5%e7%9a%84%e7%bb%b4%e6%95%b0" class="header-anchor"&gt;&lt;/a&gt;1.3 查看和改变矩阵的维数&#10;&lt;/h2&gt;&lt;p&gt;矩阵有两个维数，即行维数和列维数。在R中查看矩阵的行维数和列维数可以用函数dim()。例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; X=matrix(1:12,ncol=3,nrow=4)&#10;&#10; X&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1 5 9&#10;## [2,] 2 6 10&#10;## [3,] 3 7 11&#10;## [4,] 4 8 12&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; dim(X)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 4 3&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;只返回行维数：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; dim(X)[1]&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 4&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;也可以用函数nrow()&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;nrow(X)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 4&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;只返回列维数：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; dim(X)[2]&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 3&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;也可以用函数ncol():&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; ncol(X)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 3&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;同时，函数dim()也可以改变矩阵的维数。例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;dim(X)=c(2,6)&#10;&#10;X&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3] [,4] [,5] [,6]&#10;## [1,] 1 3 5 7 9 11&#10;## [2,] 2 4 6 8 10 12&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h2 id="14矩阵行列的名称"&gt;&lt;a href="#14%e7%9f%a9%e9%98%b5%e8%a1%8c%e5%88%97%e7%9a%84%e5%90%8d%e7%a7%b0" class="header-anchor"&gt;&lt;/a&gt;1.4矩阵行列的名称&#10;&lt;/h2&gt;&lt;p&gt;查看矩阵的行名和列名分别用函数rownames()和函数colnames()。例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;X=matrix(1:6,nrow=3,ncol=2,byrow=T,dimnames=list(c(&amp;quot;A&amp;quot;,&amp;quot;B&amp;quot;,&amp;quot;C&amp;quot;),c(&amp;quot;boy&amp;quot;,&amp;quot;girl&amp;quot;)))&#10;&#10; X&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## boy girl&#10;## A 1 2&#10;## B 3 4&#10;## C 5 6&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;查看矩阵的行名：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; rownames(X)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;A&amp;quot; &amp;quot;B&amp;quot; &amp;quot;C&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;查看矩阵的列名：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; colnames(X)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;boy&amp;quot; &amp;quot;girl&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;同时也可以改变矩阵的行名和列名，比如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;rownames(X)=c(&amp;quot;E&amp;quot;,&amp;quot;F&amp;quot;,&amp;quot;G&amp;quot;)&#10;&#10; X&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## boy girl&#10;## E 1 2&#10;## F 3 4&#10;## G 5 6&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; colnames(X)=c(&amp;quot;man&amp;quot;,&amp;quot;woman&amp;quot;)&#10;&#10; X&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## man woman&#10;## E 1 2&#10;## F 3 4&#10;## G 5 6&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h2 id="15-矩阵元素的查看及其重新赋值"&gt;&lt;a href="#15-%e7%9f%a9%e9%98%b5%e5%85%83%e7%b4%a0%e7%9a%84%e6%9f%a5%e7%9c%8b%e5%8f%8a%e5%85%b6%e9%87%8d%e6%96%b0%e8%b5%8b%e5%80%bc" class="header-anchor"&gt;&lt;/a&gt;1.5 矩阵元素的查看及其重新赋值&#10;&lt;/h2&gt;&lt;p&gt;查看矩阵的某个特定元素，只需要知道该元素的行坐标和列坐标即可，例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; X=matrix(1:12,nrow=4,ncol=3)&#10;&#10; X&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1 5 9&#10;## [2,] 2 6 10&#10;## [3,] 3 7 11&#10;## [4,] 4 8 12&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;查看位于矩阵第二行、第三列的元素：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; X[2,3]&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 10&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;也可以重新对矩阵的元素进行赋值，将矩阵第二行、第三列的元素替换为0：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; X[2,3]=0&#10;&#10; X&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1 5 9&#10;## [2,] 2 6 0&#10;## [3,] 3 7 11&#10;## [4,] 4 8 12&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;R中有一个diag()函数可以返回矩阵的全部对角元素：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; X=matrix(1:9,ncol=3,nrow=3)&#10;&#10; X&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1 4 7&#10;## [2,] 2 5 8&#10;## [3,] 3 6 9&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;diag(X)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 1 5 9&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;当然也可以对对角元素进行重新赋值：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; diag(X)=c(0,0,1)&#10;&#10; X&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 0 4 7&#10;## [2,] 2 0 8&#10;## [3,] 3 6 1&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;当操作对象不是对称矩阵时，diag()也可以进行操作。&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; X=matrix(1:12,nrow=4,ncol=3)&#10;&#10; X&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1 5 9&#10;## [2,] 2 6 10&#10;## [3,] 3 7 11&#10;## [4,] 4 8 12&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; diag(X)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 1 6 11&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;diag()函数还能用来生成对角矩阵：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; diag(c(1,2,3))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1 0 0&#10;## [2,] 0 2 0&#10;## [3,] 0 0 3&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;也可以生成单位对角矩阵：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; diag(3)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1 0 0&#10;## [2,] 0 1 0&#10;## [3,] 0 0 1&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; diag(4)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3] [,4]&#10;## [1,] 1 0 0 0&#10;## [2,] 0 1 0 0&#10;## [3,] 0 0 1 0&#10;## [4,] 0 0 0 1&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;查看矩阵的上三角部分：在R中查看矩阵的上三角和下三角部分很简单。可以通过lower.tri()和upper.tir()来实现：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; args(lower.tri)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## function (x, diag = FALSE) &#10;## NULL&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; args(upper.tri)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## function (x, diag = FALSE) &#10;## NULL&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;查看上三角：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; X=matrix(1:12,nrow=4,ncol=3)&#10;&#10; X&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1 5 9&#10;## [2,] 2 6 10&#10;## [3,] 3 7 11&#10;## [4,] 4 8 12&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; X[lower.tri(X)]&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 2 3 4 7 8 12&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;改变赋值：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; X[lower.tri(X)]=0&#10;&#10; X&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1 5 9&#10;## [2,] 0 6 10&#10;## [3,] 0 0 11&#10;## [4,] 0 0 0&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h1 id="2-矩阵计算"&gt;&lt;a href="#2-%e7%9f%a9%e9%98%b5%e8%ae%a1%e7%ae%97" class="header-anchor"&gt;&lt;/a&gt;2 矩阵计算&#10;&lt;/h1&gt;&lt;h2 id="21矩阵转置"&gt;&lt;a href="#21%e7%9f%a9%e9%98%b5%e8%bd%ac%e7%bd%ae" class="header-anchor"&gt;&lt;/a&gt;2.1矩阵转置&#10;&lt;/h2&gt;&lt;p&gt;R中矩阵的转置可以用t()函数完成，例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; X=matrix(1:12,nrow=4,ncol=3)&#10;&#10; X&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1 5 9&#10;## [2,] 2 6 10&#10;## [3,] 3 7 11&#10;## [4,] 4 8 12&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; t(X)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3] [,4]&#10;## [1,] 1 2 3 4&#10;## [2,] 5 6 7 8&#10;## [3,] 9 10 11 12&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h2 id="22矩阵的行和与列和及行平均值和列均值"&gt;&lt;a href="#22%e7%9f%a9%e9%98%b5%e7%9a%84%e8%a1%8c%e5%92%8c%e4%b8%8e%e5%88%97%e5%92%8c%e5%8f%8a%e8%a1%8c%e5%b9%b3%e5%9d%87%e5%80%bc%e5%92%8c%e5%88%97%e5%9d%87%e5%80%bc" class="header-anchor"&gt;&lt;/a&gt;2.2矩阵的行和与列和及行平均值和列均值&#10;&lt;/h2&gt;&lt;p&gt;在R中很容易计算一个矩阵的各行和和各列和以及各行的平均值和各列的平均值。例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; A=matrix(1:12,3,4)&#10;&#10; A&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3] [,4]&#10;## [1,] 1 4 7 10&#10;## [2,] 2 5 8 11&#10;## [3,] 3 6 9 12&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; rowSums(A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 22 26 30&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; rowMeans(A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 5.5 6.5 7.5&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; colSums(A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 6 15 24 33&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; colMeans(A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 2 5 8 11&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h2 id="23行列式的值"&gt;&lt;a href="#23%e8%a1%8c%e5%88%97%e5%bc%8f%e7%9a%84%e5%80%bc" class="header-anchor"&gt;&lt;/a&gt;2.3行列式的值&#10;&lt;/h2&gt;&lt;p&gt;R中的函数det()将计算方阵A的行列式。例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; X=matrix(rnorm(9),nrow=3,ncol=3)&#10;&#10; X&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] -0.6110769 -0.3434181 -1.1062389&#10;## [2,] -1.9174083 0.1106630 -0.7469723&#10;## [3,] -0.3393505 0.1931565 1.6372937&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; det(X)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] -0.9958889&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h2 id="24矩阵相加减"&gt;&lt;a href="#24%e7%9f%a9%e9%98%b5%e7%9b%b8%e5%8a%a0%e5%87%8f" class="header-anchor"&gt;&lt;/a&gt;2.4矩阵相加减&#10;&lt;/h2&gt;&lt;p&gt;矩阵元素的相加减是指维数相同的矩阵，处于同行和同列的位置的元素进行加减。实现这个功能用“＋”，“－”即可。例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; A=B=matrix(1:12,nrow=3,ncol=4)&#10;&#10; A+B&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3] [,4]&#10;## [1,] 2 8 14 20&#10;## [2,] 4 10 16 22&#10;## [3,] 6 12 18 24&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; A-B&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3] [,4]&#10;## [1,] 0 0 0 0&#10;## [2,] 0 0 0 0&#10;## [3,] 0 0 0 0&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h2 id="25矩阵的数乘"&gt;&lt;a href="#25%e7%9f%a9%e9%98%b5%e7%9a%84%e6%95%b0%e4%b9%98" class="header-anchor"&gt;&lt;/a&gt;2.5矩阵的数乘&#10;&lt;/h2&gt;&lt;p&gt;矩阵的数乘是指一个常数与一个矩阵相乘。设A为 &lt;code&gt;\(m\times n\)&lt;/code&gt; 矩阵，当 &lt;code&gt;\(c\ne0\)&lt;/code&gt; 时，在R中求 &lt;code&gt;\(c*A\)&lt;/code&gt; 的值，可以用符号“*”。例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; c=2&#10;&#10; A=matrix(1:12,nrow=3,ncol=4)&#10;&#10; A&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3] [,4]&#10;## [1,] 1 4 7 10&#10;## [2,] 2 5 8 11&#10;## [3,] 3 6 9 12&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; c*A&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3] [,4]&#10;## [1,] 2 8 14 20&#10;## [2,] 4 10 16 22&#10;## [3,] 6 12 18 24&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;结果矩阵与原矩阵的所有相应元素都差一个常数c。&lt;/p&gt;&#10;&lt;h2 id="26矩阵相乘"&gt;&lt;a href="#26%e7%9f%a9%e9%98%b5%e7%9b%b8%e4%b9%98" class="header-anchor"&gt;&lt;/a&gt;2.6矩阵相乘&#10;&lt;/h2&gt;&lt;h3 id="261矩阵的乘法"&gt;&lt;a href="#261%e7%9f%a9%e9%98%b5%e7%9a%84%e4%b9%98%e6%b3%95" class="header-anchor"&gt;&lt;/a&gt;2.6.1矩阵的乘法&#10;&lt;/h3&gt;&lt;p&gt;A为 &lt;code&gt;\(m\times n\)&lt;/code&gt; 矩阵，B为 &lt;code&gt;\(n\times k\)&lt;/code&gt; 矩阵，在R中求 &lt;code&gt;\(A\times B\)&lt;/code&gt; ，可以符号“%*%”。例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; A=matrix(1:12,nrow=3,ncol=4)&#10;&#10; B=matrix(1:12,nrow=4,ncol=3)&#10;&#10; A%*%B&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 70 158 246&#10;## [2,] 80 184 288&#10;## [3,] 90 210 330&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;注意: &lt;code&gt;\(B\times A\)&lt;/code&gt; 无意义，因其不符合矩阵的相乘规则。&lt;/p&gt;&#10;&lt;p&gt;若A为 &lt;code&gt;\(n\times m\)&lt;/code&gt; 矩阵，B为 &lt;code&gt;\(n\times k\)&lt;/code&gt; 矩阵，在R中求 &lt;code&gt;\(A^{'}\times B\)&lt;/code&gt; ：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; A=matrix(1:12,nrow=4,ncol=3)&#10;&#10; B=matrix(1:12,nrow=4,ncol=3)&#10;&#10; t(A)%*%B&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 30 70 110&#10;## [2,] 70 174 278&#10;## [3,] 110 278 446&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;也可以用函数crossprod()计算 &lt;code&gt;\(A^{'}\times B\)&lt;/code&gt; ：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;crossprod(A,B)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 30 70 110&#10;## [2,] 70 174 278&#10;## [3,] 110 278 446&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h3 id="262矩阵的kronecker积"&gt;&lt;a href="#262%e7%9f%a9%e9%98%b5%e7%9a%84kronecker%e7%a7%af" class="header-anchor"&gt;&lt;/a&gt;2.6.2矩阵的Kronecker积&#10;&lt;/h3&gt;&lt;p&gt;&lt;code&gt;\(n\times m\)&lt;/code&gt; 的矩阵A和 &lt;code&gt;\(h\times k\)&lt;/code&gt; 的矩阵B的Kronecker积是一个 &lt;code&gt;\(nh\times mk\)&lt;/code&gt; 维矩阵，公式为：&lt;/p&gt;&#10;&lt;p&gt;$$&#10;A \otimes B = \begin{bmatrix}&#10;a_{11}B &amp;amp; a_{12}B &amp;amp; \cdots &amp;amp; a_{1m}B \&#10;a_{21}B &amp;amp; a_{22}B &amp;amp; \cdots &amp;amp; a_{2m}B \&#10;\vdots &amp;amp; \vdots &amp;amp; \ddots &amp;amp; \vdots \&#10;a_{n1}B &amp;amp; a_{n2}B &amp;amp; \cdots &amp;amp; a_{nm}B&#10;\end{bmatrix}&#10;$$&lt;/p&gt;&#10;&lt;p&gt;其中：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;\(a_{ij}\)&lt;/code&gt; 表示矩阵A的第i行第j列元素&lt;/li&gt;&#10;&lt;li&gt;每个分块 &lt;code&gt;\(a_{ij}B\)&lt;/code&gt; 是原矩阵B的所有元素按 &lt;code&gt;\(a_{ij}\)&lt;/code&gt; 比例缩放后的子矩阵&lt;/li&gt;&#10;&lt;li&gt;最终结果矩阵的维度为 &lt;code&gt;\((n \cdot h) \times (m \cdot k)\)&lt;/code&gt;，即原矩阵行数乘积和新矩阵列数乘积&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;在R中Kronecker积可以用函数kronecher()来计算。例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; A=matrix(1:4,2,2)&#10;&#10; A&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2]&#10;## [1,] 1 3&#10;## [2,] 2 4&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; B=matrix(rep(1,4),2,2)&#10;&#10; B&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2]&#10;## [1,] 1 1&#10;## [2,] 1 1&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; kronecker(A,B)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3] [,4]&#10;## [1,] 1 1 3 3&#10;## [2,] 1 1 3 3&#10;## [3,] 2 2 4 4&#10;## [4,] 2 2 4 4&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h2 id="27矩阵的伴随矩阵"&gt;&lt;a href="#27%e7%9f%a9%e9%98%b5%e7%9a%84%e4%bc%b4%e9%9a%8f%e7%9f%a9%e9%98%b5" class="header-anchor"&gt;&lt;/a&gt;2.7矩阵的伴随矩阵&#10;&lt;/h2&gt;&lt;p&gt;求矩阵A的伴随矩阵可以用LoopAnalyst包中的函数make.adjoint()函数。例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; A=matrix(1:12,nrow=3,ncol=4)&#10;&#10; A&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3] [,4]&#10;## [1,] 1 4 7 10&#10;## [2,] 2 5 8 11&#10;## [3,] 3 6 9 12&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; make.adjoint(A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] -3 6 -3&#10;## [2,] 6 -12 6&#10;## [3,] -3 6 -3&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h2 id="28矩阵的逆和广义逆"&gt;&lt;a href="#28%e7%9f%a9%e9%98%b5%e7%9a%84%e9%80%86%e5%92%8c%e5%b9%bf%e4%b9%89%e9%80%86" class="header-anchor"&gt;&lt;/a&gt;2.8矩阵的逆和广义逆&#10;&lt;/h2&gt;&lt;h3 id="281矩阵的逆"&gt;&lt;a href="#281%e7%9f%a9%e9%98%b5%e7%9a%84%e9%80%86" class="header-anchor"&gt;&lt;/a&gt;2.8.1矩阵的逆&#10;&lt;/h3&gt;&lt;p&gt;矩阵A的逆A-1可以用函数solve()，例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; A=matrix(rnorm(9),nrow=3,ncol=3)&#10;&#10; A&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 0.1630739 0.6387177 0.5308319&#10;## [2,] -0.8614037 0.3068824 -0.2521929&#10;## [3,] -1.4320496 0.4783913 -0.6097321&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; solve(A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 0.6574602 -6.363927 3.204585&#10;## [2,] 1.6228775 -6.535581 4.116074&#10;## [3,] -0.2708487 9.818892 -5.937084&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;验证AA-1=1：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; A%*%solve(A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1.000000e+00 8.881784e-16 -4.440892e-16&#10;## [2,] -6.938894e-17 1.000000e+00 -2.220446e-16&#10;## [3,] -2.775558e-17 -8.881784e-16 1.000000e+00&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;用round函数可以更好的得到结果：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; round(A%*%solve(A))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1 0 0&#10;## [2,] 0 1 0&#10;## [3,] 0 0 1&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;solve()函数也可以用来求解方程组ax=b。&lt;/p&gt;&#10;&lt;h3 id="282矩阵的广义逆moore-penrose"&gt;&lt;a href="#282%e7%9f%a9%e9%98%b5%e7%9a%84%e5%b9%bf%e4%b9%89%e9%80%86moore-penrose" class="header-anchor"&gt;&lt;/a&gt;2.8.2矩阵的广义逆（Moore-Penrose）&#10;&lt;/h3&gt;&lt;p&gt;&lt;code&gt;\(n \times m\)&lt;/code&gt; 矩阵 &lt;code&gt;\(A^+\)&lt;/code&gt; 称为矩阵 &lt;code&gt;\(A\)&lt;/code&gt; 的 Moore-Penrose 广义逆，当且仅当它同时满足以下四个条件:&lt;/p&gt;&#10;&lt;p&gt;1.&lt;strong&gt;相容性条件&lt;/strong&gt;&#10;$$ AA^+A = A $$&lt;br&gt;&#10;该条件保证了广义逆与原矩阵相乘后能保持原矩阵的核心结构不变。&lt;/p&gt;&#10;&lt;p&gt;2.&lt;strong&gt;弱相容性条件&lt;/strong&gt;&#10;$$ A^+AA^+ = A^+ $$&lt;br&gt;&#10;表明广义逆的迭代运算具有稳定性；&lt;/p&gt;&#10;&lt;p&gt;3.&lt;strong&gt;对称性条件&lt;/strong&gt;&#10;$$ (AA^+)^T = AA^+ $$&lt;br&gt;&#10;确保乘积矩阵 &lt;code&gt;\(AA^+\)&lt;/code&gt; 是正交投影算子；&lt;/p&gt;&#10;&lt;p&gt;4.&lt;strong&gt;共轭对称性条件&lt;/strong&gt;&#10;$$ (A^+A)^T = A^+A $$&lt;br&gt;&#10;保证乘积矩阵 &lt;code&gt;\(A^+A\)&lt;/code&gt; 同样是正交投影算子。&lt;/p&gt;&#10;&lt;p&gt;其核心特性包括：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;存在唯一性：任意矩阵都存在唯一的 Moore-Penrose 逆；&lt;/li&gt;&#10;&lt;li&gt;退化特性：当 &lt;code&gt;\(A\)&lt;/code&gt; 可逆时，$A^+ = A^{-1}$；&lt;/li&gt;&#10;&lt;li&gt;计算方式：可通过奇异值分解实现 $$ A^+ = V\Sigma^+U^T $$ 其中 &lt;code&gt;\(\Sigma^+\)&lt;/code&gt; 为奇异值矩阵的伪逆。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;R中MASS包中的ginv()函数可以计算矩阵的Moore-Penrose逆。例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; library(MASS)&#10;&#10; A=matrix(1:12,nrow=3,ncol=4)&#10;&#10; A&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3] [,4]&#10;## [1,] 1 4 7 10&#10;## [2,] 2 5 8 11&#10;## [3,] 3 6 9 12&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; # solve(A) #出错&#10;&#10; ginv(A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] -0.483333333 -0.03333333 0.41666667&#10;## [2,] -0.244444444 -0.01111111 0.22222222&#10;## [3,] -0.005555556 0.01111111 0.02777778&#10;## [4,] 0.233333333 0.03333333 -0.16666667&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;验证性质1：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; A%*%ginv(A)%*%A&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3] [,4]&#10;## [1,] 1 4 7 10&#10;## [2,] 2 5 8 11&#10;## [3,] 3 6 9 12&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; A&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3] [,4]&#10;## [1,] 1 4 7 10&#10;## [2,] 2 5 8 11&#10;## [3,] 3 6 9 12&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;验证性质2：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; ginv(A)%*%A%*%ginv(A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] -0.483333333 -0.03333333 0.41666667&#10;## [2,] -0.244444444 -0.01111111 0.22222222&#10;## [3,] -0.005555556 0.01111111 0.02777778&#10;## [4,] 0.233333333 0.03333333 -0.16666667&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; ginv(A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] -0.483333333 -0.03333333 0.41666667&#10;## [2,] -0.244444444 -0.01111111 0.22222222&#10;## [3,] -0.005555556 0.01111111 0.02777778&#10;## [4,] 0.233333333 0.03333333 -0.16666667&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;验证性质3：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; A%*%ginv(A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 0.8333333 0.3333333 -0.1666667&#10;## [2,] 0.3333333 0.3333333 0.3333333&#10;## [3,] -0.1666667 0.3333333 0.8333333&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; t(A%*%ginv(A))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 0.8333333 0.3333333 -0.1666667&#10;## [2,] 0.3333333 0.3333333 0.3333333&#10;## [3,] -0.1666667 0.3333333 0.8333333&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;验证性质4：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; ginv(A)%*%A&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3] [,4]&#10;## [1,] 0.7 0.4 0.1 -0.2&#10;## [2,] 0.4 0.3 0.2 0.1&#10;## [3,] 0.1 0.2 0.3 0.4&#10;## [4,] -0.2 0.1 0.4 0.7&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; t(ginv(A)%*%A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3] [,4]&#10;## [1,] 0.7 0.4 0.1 -0.2&#10;## [2,] 0.4 0.3 0.2 0.1&#10;## [3,] 0.1 0.2 0.3 0.4&#10;## [4,] -0.2 0.1 0.4 0.7&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;也可以不必如此麻烦来验证性质3和4，因为3和4只是表明 &lt;code&gt;\(AA^+\)&lt;/code&gt; 和 &lt;code&gt;\(A^+A\)&lt;/code&gt; 是对称矩阵。&lt;/p&gt;&#10;&lt;h3 id="283-xx-的逆"&gt;&lt;a href="#283-xx-%e7%9a%84%e9%80%86" class="header-anchor"&gt;&lt;/a&gt;2.8.3 [X^&amp;lsquo;X ]的逆&#10;&lt;/h3&gt;&lt;p&gt;很多时候，我们需要计算形如 &lt;code&gt;\(X^{'}X\)&lt;/code&gt; 的逆。这很容易实现，例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; x=matrix(rnorm(9),ncol=3,nrow=3)&#10;&#10; x&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1.8093460 1.26990532 -1.3654367&#10;## [2,] 0.4774751 0.21718498 0.2193708&#10;## [3,] -0.9058385 0.06350197 0.6108242&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; solve(crossprod(x))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 2.112090 -1.0630514 1.9311835&#10;## [2,] -1.063051 2.6346674 0.5414491&#10;## [3,] 1.931184 0.5414491 3.2942094&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;R中的strucchange包中的函数solveCrossprod()也可完成：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; library(strucchange)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：zoo&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## 载入程序包：'zoo'&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from 'package:base':&#10;## &#10;## as.Date, as.Date.numeric&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：sandwich&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; args(solveCrossprod)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## function (X, method = c(&amp;quot;qr&amp;quot;, &amp;quot;chol&amp;quot;, &amp;quot;solve&amp;quot;)) &#10;## NULL&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; solveCrossprod(x,method=&amp;quot;qr&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 2.112090 -1.0630514 1.9311835&#10;## [2,] -1.063051 2.6346674 0.5414491&#10;## [3,] 1.931184 0.5414491 3.2942094&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; solveCrossprod(x,method=&amp;quot;chol&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 2.112090 -1.0630514 1.9311835&#10;## [2,] -1.063051 2.6346674 0.5414491&#10;## [3,] 1.931184 0.5414491 3.2942094&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; solveCrossprod(x,method=&amp;quot;solve&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 2.112090 -1.0630514 1.9311835&#10;## [2,] -1.063051 2.6346674 0.5414491&#10;## [3,] 1.931184 0.5414491 3.2942094&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h2 id="29矩阵的特征值和特征向量"&gt;&lt;a href="#29%e7%9f%a9%e9%98%b5%e7%9a%84%e7%89%b9%e5%be%81%e5%80%bc%e5%92%8c%e7%89%b9%e5%be%81%e5%90%91%e9%87%8f" class="header-anchor"&gt;&lt;/a&gt;2.9矩阵的特征值和特征向量&#10;&lt;/h2&gt;&lt;p&gt;矩阵的谱分解（或称特征分解）是将方阵分解为特征值和特征向量构成的正交投影矩阵的线性组合。设 &lt;code&gt;\(A\)&lt;/code&gt; 是 &lt;code&gt;\(n \times n\)&lt;/code&gt; 的&lt;strong&gt;正规矩阵&lt;/strong&gt;（例如实对称矩阵、Hermitian矩阵或酉矩阵），其谱分解公式为：&lt;/p&gt;&#10;&lt;p&gt;$$&#10;A=\sum_{i=1}^{k}\lambda_{i}E_{i}&#10;$$&lt;/p&gt;&#10;&lt;p&gt;其中：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;\(\lambda_i\)&lt;/code&gt; 是 &lt;code&gt;\(A\)&lt;/code&gt; 的&lt;strong&gt;互异特征值&lt;/strong&gt;（共 &lt;code&gt;\(k\)&lt;/code&gt; 个不同的特征值）；&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;\(E_i\)&lt;/code&gt; 是到 &lt;code&gt;\(\lambda_i\)&lt;/code&gt; 对应特征空间的&lt;strong&gt;正交投影矩阵&lt;/strong&gt;，满足：&#10;-&lt;strong&gt;正交性&lt;/strong&gt;：$E_i E_j = 0 \ (\forall i \neq j)$&#10;-&lt;strong&gt;幂等性&lt;/strong&gt;：$E_i^2 = E_i$&#10;-&lt;strong&gt;单位分解&lt;/strong&gt;：$\sum_{i=1}^k E_i = I_n$&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h3 id="关键条件与性质"&gt;&lt;a href="#%e5%85%b3%e9%94%ae%e6%9d%a1%e4%bb%b6%e4%b8%8e%e6%80%a7%e8%b4%a8" class="header-anchor"&gt;&lt;/a&gt;关键条件与性质：&#10;&lt;/h3&gt;&lt;p&gt;1.&lt;strong&gt;可对角化性&lt;/strong&gt;：谱分解要求矩阵 &lt;code&gt;\(A\)&lt;/code&gt; 可对角化，正规矩阵（如对称矩阵、Hermitian矩阵）一定满足此条件；&#10;2.&lt;strong&gt;唯一性&lt;/strong&gt;：分解形式唯一，由特征值和对应的正交投影矩阵完全确定；&#10;3.&lt;strong&gt;几何意义&lt;/strong&gt;：$E_i$ 将向量投影到 &lt;code&gt;\(\lambda_i\)&lt;/code&gt; 的特征空间，$A$ 的作用相当于在不同特征方向上按 &lt;code&gt;\(\lambda_i\)&lt;/code&gt; 缩放；&#10;4.&lt;strong&gt;特殊情形&lt;/strong&gt;：若 &lt;code&gt;\(A\)&lt;/code&gt; 的特征值互异（无重复），则 &lt;code&gt;\(E_i = v_i v_i^T / \|v_i\|^2\)&lt;/code&gt;（$v_i$ 为标准正交特征向量）。&lt;/p&gt;&#10;&lt;p&gt;比如：&lt;/p&gt;&#10;&lt;p&gt;若 &lt;code&gt;\(A = \begin{bmatrix} 2 &amp;amp; 1 \\ 1 &amp;amp; 2 \end{bmatrix}\)&lt;/code&gt;，其特征值 &lt;code&gt;\(\lambda_1=3\)&lt;/code&gt;, &lt;code&gt;\(\lambda_2=1\)&lt;/code&gt;，对应投影矩阵：&lt;/p&gt;&#10;&lt;p&gt;$$&#10;E_1 = \frac{1}{2}\begin{bmatrix} 1 &amp;amp; 1 \ 1 &amp;amp; 1 \end{bmatrix}, \quad E_2 = \frac{1}{2}\begin{bmatrix} 1 &amp;amp; -1 \ -1 &amp;amp; 1 \end{bmatrix}&#10;$$&lt;/p&gt;&#10;&lt;p&gt;则谱分解为：&lt;/p&gt;&#10;&lt;p&gt;$$&#10;A = 3E_1 + 1E_2&#10;$$&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; args(eigen)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## function (x, symmetric, only.values = FALSE, EISPACK = FALSE) &#10;## NULL&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;其中，x参数输入矩阵；symmetric参数判断矩阵是否为对称矩阵，如果参数为空，系统将自动检测矩阵的对称性。例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; A=matrix(1:9,nrow=3,ncol=3)&#10; A&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1 4 7&#10;## [2,] 2 5 8&#10;## [3,] 3 6 9&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; Aeigen=eigen(A)&#10;&#10; Aeigen&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## eigen() decomposition&#10;## $values&#10;## [1] 1.611684e+01 -1.116844e+00 -5.700691e-16&#10;## &#10;## $vectors&#10;## [,1] [,2] [,3]&#10;## [1,] -0.4645473 -0.8829060 0.4082483&#10;## [2,] -0.5707955 -0.2395204 -0.8164966&#10;## [3,] -0.6770438 0.4038651 0.4082483&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;得到矩阵A的特征值：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; Aeigen$values&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 1.611684e+01 -1.116844e+00 -5.700691e-16&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;得到矩阵A的特征向量：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; Aeigen$vectors&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] -0.4645473 -0.8829060 0.4082483&#10;## [2,] -0.5707955 -0.2395204 -0.8164966&#10;## [3,] -0.6770438 0.4038651 0.4082483&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h1 id="3-矩阵高级操作"&gt;&lt;a href="#3-%e7%9f%a9%e9%98%b5%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c" class="header-anchor"&gt;&lt;/a&gt;3 矩阵高级操作&#10;&lt;/h1&gt;&lt;h2 id="31-choleskey分解"&gt;&lt;a href="#31-choleskey%e5%88%86%e8%a7%a3" class="header-anchor"&gt;&lt;/a&gt;3.1 Choleskey分解&#10;&lt;/h2&gt;&lt;p&gt;对于正定矩阵A，可以对其进行Choleskey分解，$A=P^{&amp;rsquo;}P$ ，其中P为上三角矩阵，在R中可以用函数chol()。例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; A=diag(3)+1&#10;&#10; A&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 2 1 1&#10;## [2,] 1 2 1&#10;## [3,] 1 1 2&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; chol(A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1.414214 0.7071068 0.7071068&#10;## [2,] 0.000000 1.2247449 0.4082483&#10;## [3,] 0.000000 0.0000000 1.1547005&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;验证$A=P^{&amp;rsquo;}P$ ：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; t(chol(A))%*%chol(A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 2 1 1&#10;## [2,] 1 2 1&#10;## [3,] 1 1 2&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;也可以用crossprod()函数：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; crossprod(chol(A),chol(A))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 2 1 1&#10;## [2,] 1 2 1&#10;## [3,] 1 1 2&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;可以用Choleskey分解来计算矩阵的行列式：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; prod(diag(chol(A))^2)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 4&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; det(A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 4&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;也可以用Choleskey分解来计算矩阵的逆，这时候可以用到函数chol2inv():&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; chol2inv(chol(A))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 0.75 -0.25 -0.25&#10;## [2,] -0.25 0.75 -0.25&#10;## [3,] -0.25 -0.25 0.75&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; solve(A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 0.75 -0.25 -0.25&#10;## [2,] -0.25 0.75 -0.25&#10;## [3,] -0.25 -0.25 0.75&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h2 id="32-奇异值分解"&gt;&lt;a href="#32-%e5%a5%87%e5%bc%82%e5%80%bc%e5%88%86%e8%a7%a3" class="header-anchor"&gt;&lt;/a&gt;3.2 奇异值分解&#10;&lt;/h2&gt;&lt;p&gt;A为$m\times n$ 矩阵，矩阵的秩为r。A可以分解为$A=UDV^{&amp;rsquo;}$ ，其中:$U^{&amp;rsquo;}U=V^{&amp;rsquo;}V=I$ 。在R中可以用函数svd()来完成奇异值分解。&lt;/p&gt;&#10;&lt;p&gt;例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; A=matrix(1:18,3,6)&#10;&#10; A&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3] [,4] [,5] [,6]&#10;## [1,] 1 4 7 10 13 16&#10;## [2,] 2 5 8 11 14 17&#10;## [3,] 3 6 9 12 15 18&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; svd(A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## $d&#10;## [1] 4.589453e+01 1.640705e+00 1.366522e-15&#10;## &#10;## $u&#10;## [,1] [,2] [,3]&#10;## [1,] -0.5290354 0.74394551 0.4082483&#10;## [2,] -0.5760715 0.03840487 -0.8164966&#10;## [3,] -0.6231077 -0.66713577 0.4082483&#10;## &#10;## $v&#10;## [,1] [,2] [,3]&#10;## [1,] -0.07736219 -0.71960032 -0.4076688&#10;## [2,] -0.19033085 -0.50893247 0.5745647&#10;## [3,] -0.30329950 -0.29826463 -0.0280114&#10;## [4,] -0.41626816 -0.08759679 0.2226621&#10;## [5,] -0.52923682 0.12307105 -0.6212052&#10;## [6,] -0.64220548 0.33373889 0.2596585&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; svd(A)$u%*%diag(svd(A)$d)%*%t(svd(A)$v)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3] [,4] [,5] [,6]&#10;## [1,] 1 4 7 10 13 16&#10;## [2,] 2 5 8 11 14 17&#10;## [3,] 3 6 9 12 15 18&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h2 id="33-qr分解"&gt;&lt;a href="#33-qr%e5%88%86%e8%a7%a3" class="header-anchor"&gt;&lt;/a&gt;3.3 QR分解&#10;&lt;/h2&gt;&lt;p&gt;A为$m\times n$ 矩阵可以进行QR分解:$A=QR$ ，其中$Q^{&amp;rsquo;}Q=I$ ，在R中可以用函数qr()来完成这个过程，例如：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; A=matrix(1:12,4,3)&#10;&#10; qr(A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## $qr&#10;## [,1] [,2] [,3]&#10;## [1,] -5.4772256 -12.7801930 -2.008316e+01&#10;## [2,] 0.3651484 -3.2659863 -6.531973e+00&#10;## [3,] 0.5477226 -0.3781696 1.601186e-15&#10;## [4,] 0.7302967 -0.9124744 -5.547002e-01&#10;## &#10;## $rank&#10;## [1] 2&#10;## &#10;## $qraux&#10;## [1] 1.182574 1.156135 1.832050&#10;## &#10;## $pivot&#10;## [1] 1 2 3&#10;## &#10;## attr(,&amp;quot;class&amp;quot;)&#10;## [1] &amp;quot;qr&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;Rank返回的是矩阵的秩。Qr项包含了Q矩阵和R矩阵的信息。要想得到Q矩阵和R矩阵，可以用qr.Q()函数和qr.R()函数：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;qr.Q(qr(A))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] -0.1825742 -8.164966e-01 -0.4000874&#10;## [2,] -0.3651484 -4.082483e-01 0.2546329&#10;## [3,] -0.5477226 -1.665335e-16 0.6909965&#10;## [4,] -0.7302967 4.082483e-01 -0.5455419&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;qr.R(qr(A))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] -5.477226 -12.780193 -2.008316e+01&#10;## [2,] 0.000000 -3.265986 -6.531973e+00&#10;## [3,] 0.000000 0.000000 1.601186e-15&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h1 id="4-解方程组"&gt;&lt;a href="#4-%e8%a7%a3%e6%96%b9%e7%a8%8b%e7%bb%84" class="header-anchor"&gt;&lt;/a&gt;4 解方程组&#10;&lt;/h1&gt;&lt;h2 id="41普通方程组"&gt;&lt;a href="#41%e6%99%ae%e9%80%9a%e6%96%b9%e7%a8%8b%e7%bb%84" class="header-anchor"&gt;&lt;/a&gt;4.1普通方程组&#10;&lt;/h2&gt;&lt;p&gt;解普通方程组可以用函数solve()，solve()的基本用法是solve(A,b)，其中，A为方程组的系数矩阵，b为方程组的右端。例如：&lt;/p&gt;&#10;&lt;p&gt;已知方程组：&lt;/p&gt;&#10;&lt;p&gt;$$&#10;\begin{cases}&#10;2x_1+2x_3=1\&#10;2x_1+x_2+2x_3=2\&#10;2x_1+x_2=3&#10;\end{cases}&#10;$$&lt;/p&gt;&#10;&lt;p&gt;解法如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;A=matrix(&#10;c(2,0,2,&#10; 2,1,2,&#10; 2,1,0),&#10; byrow=TRUE,nrow=3&#10;)&#10;&#10;b=1:3&#10;&#10;b&#10;&#10;solve(A,b)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;即$x_1=1$ ，$x_2=1$ ，$x_3=-0.5$ 。&lt;/p&gt;&#10;&lt;h2 id="42-特殊方程组"&gt;&lt;a href="#42-%e7%89%b9%e6%ae%8a%e6%96%b9%e7%a8%8b%e7%bb%84" class="header-anchor"&gt;&lt;/a&gt;4.2 特殊方程组&#10;&lt;/h2&gt;&lt;p&gt;对于系数矩阵是上三角矩阵和下三角矩阵的方程组。R中提供了backsolve()和fowardsolve()来解决这个问题。&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;backsolve(r, x, k=ncol(r), upper.tri=TRUE, transpose=FALSE)&#10;&#10;forwardsolve(l, x, k=ncol(l), upper.tri=FALSE, transpose=FALSE)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;这两个函数都是符合操作的函数，大致可以分为三个步骤：&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;通过将系数矩阵的上三角或者下三角变为0的到新的系数矩阵,这通过upper.tri参数来实现，若upper.tri=TRUR，上三角不为0。&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;通过将对步骤1中得到的新系数矩阵进行转置得到新的系数矩阵，这通过transpose参数实现，若transpose=FALSE，则步骤1中得到的系数矩阵将被转置。&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;根据步骤2得到的系数矩阵来解方程组。&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;$$&#10;\begin{cases}&#10;X_1+4X_2+7X_3=1\&#10;2X_1+5X_2+8X_3=2\&#10;3X_1+6X_2+9X_3=3&#10;\end{cases}&#10;$$&lt;/p&gt;&#10;&lt;p&gt;方程组的系数矩阵为：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;A &amp;lt;- matrix(&#10; c(1,4,7,&#10; 2,5,8,&#10; 3,6,9),&#10; byrow=TRUE,&#10; nrow = 3&#10;)&#10;&#10;b=1:3&#10;&#10;backsolve(A,b,upper.tri=T,transpose=F)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] -0.8000000 -0.1333333 0.3333333&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;过程分解：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;upper.tri=T，说明系数矩阵的上三角不为0。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;B=A&#10;&#10;B[lower.tri(B)]=0&#10;&#10;B&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1 4 7&#10;## [2,] 0 5 8&#10;## [3,] 0 0 9&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;transpose=F说明系数矩阵未被转置。&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;解方程：&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;solve(B,b)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] -0.8000000 -0.1333333 0.3333333&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h1 id="5-其它"&gt;&lt;a href="#5-%e5%85%b6%e5%ae%83" class="header-anchor"&gt;&lt;/a&gt;5 其它&#10;&lt;/h1&gt;&lt;h2 id="51矩阵的向量化"&gt;&lt;a href="#51%e7%9f%a9%e9%98%b5%e7%9a%84%e5%90%91%e9%87%8f%e5%8c%96" class="header-anchor"&gt;&lt;/a&gt;5.1矩阵的向量化&#10;&lt;/h2&gt;&lt;p&gt;将矩阵向量化有时候是必要的。矩阵的向量化可以通过as.vector()来实现：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;A &amp;lt;- matrix(1:9,nrow=3)&#10;&#10;A&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1 4 7&#10;## [2,] 2 5 8&#10;## [3,] 3 6 9&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;将矩阵元素向量化：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;as.vector(A)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 1 2 3 4 5 6 7 8 9&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;A&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1 4 7&#10;## [2,] 2 5 8&#10;## [3,] 3 6 9&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;将矩阵的方阵部分元素向量化：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;as.vector(A[1:min(dim(A)),1:min(dim(A))])&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 1 2 3 4 5 6 7 8 9&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h2 id="52矩阵的合并"&gt;&lt;a href="#52%e7%9f%a9%e9%98%b5%e7%9a%84%e5%90%88%e5%b9%b6" class="header-anchor"&gt;&lt;/a&gt;5.2矩阵的合并&#10;&lt;/h2&gt;&lt;h3 id="521矩阵的列合并"&gt;&lt;a href="#521%e7%9f%a9%e9%98%b5%e7%9a%84%e5%88%97%e5%90%88%e5%b9%b6" class="header-anchor"&gt;&lt;/a&gt;5.2.1矩阵的列合并&#10;&lt;/h3&gt;&lt;p&gt;矩阵的列合并可以通过cbind()来实现。&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;A&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1 4 7&#10;## [2,] 2 5 8&#10;## [3,] 3 6 9&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;B=1:3&#10;&#10;cbind(A,B)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## B&#10;## [1,] 1 4 7 1&#10;## [2,] 2 5 8 2&#10;## [3,] 3 6 9 3&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h3 id="522矩阵的行合并"&gt;&lt;a href="#522%e7%9f%a9%e9%98%b5%e7%9a%84%e8%a1%8c%e5%90%88%e5%b9%b6" class="header-anchor"&gt;&lt;/a&gt;5.2.2矩阵的行合并&#10;&lt;/h3&gt;&lt;p&gt;矩阵的行合并可以通过rbind()来实现。&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;A&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## [1,] 1 4 7&#10;## [2,] 2 5 8&#10;## [3,] 3 6 9&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;B=1:3&#10;&#10;rbind(A,B)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3]&#10;## 1 4 7&#10;## 2 5 8&#10;## 3 6 9&#10;## B 1 2 3&#10;&lt;/code&gt;&lt;/pre&gt;&#10;</description></item></channel></rss>