1 鍒涘缓涓€涓悜閲?/strong>
鍦≧涓彲浠ョ敤鍑芥暟c()鏉ュ垱寤轰竴涓悜閲忥紝渚嬪锛?br />> x=c(1,2,3,4)
> x
[1] 1 2 3 4
2 鍒涘缓涓€涓煩闃?/strong>
鍦≧涓彲浠ョ敤鍑芥暟matrix()鏉ュ垱寤轰竴涓煩闃碉紝搴旂敤璇ュ嚱鏁版椂闇€瑕佽緭鍏ュ繀瑕佺殑鍙傛暟鍊笺€?br />> args(matrix)
function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
data椤逛负蹇呰鐨勭煩闃靛厓绱狅紝nrow涓鸿鏁帮紝ncol涓哄垪鏁帮紝娉ㄦ剰nrow涓?font face="Courier New, Courier, Monospace">ncol鐨勪箻绉簲涓虹煩闃靛厓绱犱釜鏁帮紝byrow椤规帶鍒舵帓鍒楀厓绱犳椂鏄惁鎸夎杩涜锛?font face="Courier New, Courier, Monospace">dimnames缁欏畾琛屽拰鍒楃殑鍚嶇О銆備緥濡傦細
> matrix(1:12,nrow=3,ncol=4)
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> matrix(1:12,nrow=4,ncol=3)
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
> matrix(1:12,nrow=4,ncol=3,byrow=T)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12
> rowname
[1] "r1" "r2" "r3"
> colname=c("c1","c2","c3","c4")
> colname
[1] "c1" "c2" "c3" "c4"
> matrix(1:12,nrow=3,ncol=4,dimnames=list(rowname,colname))
c1 c2 c3 c4
r1 1 4 7 10
r2 2 5 8 11
3 鐭╅樀杞疆
A涓簃×n鐭╅樀锛屾眰A'鍦?font face="Courier New, Courier, Monospace">R涓彲鐢ㄥ嚱鏁?font face="Courier New, Courier, Monospace">t()锛?/font>渚嬪锛?br />> A=matrix(1:12,nrow=3,ncol=4)
> A
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> t(A)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12
鑻ュ皢鍑芥暟t()浣滅敤浜庝竴涓悜閲弜锛屽垯R榛樿x涓哄垪鍚戦噺锛岃繑鍥炵粨鏋滀负涓€涓鍚戦噺锛屼緥濡傦細
> x
[1] 1 2 3 4 5 6 7 8 9 10
> t(x)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 2 3 4 5 6 7 8 9 10
> class(x)
[1] "integer"
> class(t(x))
[1] "matrix"
鑻ユ兂寰楀埌涓€涓垪鍚戦噺锛屽彲鐢?font face="Courier New, Courier, Monospace">t(t(x))锛?/font>渚嬪锛?br />> x
[1] 1 2 3 4 5 6 7 8 9 10
> t(t(x))
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
[7,] 7
[8,] 8
[9,] 9
[10,] 10
> y=t(t(x))
> t(t(y))
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
[7,] 7
[8,] 8
[9,] 9
[10,] 10
4 鐭╅樀鐩稿姞鍑?/strong>
鍦≧涓鍚岃鍚屽垪鐭╅樀鐩稿姞鍑忥紝鍙敤绗﹀彿锛?ldquo;锛?rdquo;銆?ldquo;锛?rdquo;锛屼緥濡傦細
> A=B=matrix(1:12,nrow=3,ncol=4)
> A+B
[,1] [,2] [,3] [,4]
[1,] 2 8 14 20
[2,] 4 10 16 22
[3,] 6 12 18 24
> A-B
[,1] [,2] [,3] [,4]
[1,] 0 0 0 0
[2,] 0 0 0 0
[3,] 0 0 0 0
5 鏁颁笌鐭╅樀鐩镐箻
A涓簃×n鐭╅樀锛宑>0锛屽湪R涓眰cA鍙敤绗﹀彿锛?ldquo;*”锛屼緥濡傦細
> c=2
> c*A
[,1] [,2] [,3] [,4]
[1,] 2 8 14 20
[2,] 4 10 16 22
[3,] 6 12 18 24
6 鐭╅樀鐩镐箻
A涓簃×n鐭╅樀锛孊涓簄×k鐭╅樀锛屽湪R涓眰AB鍙敤绗﹀彿锛?ldquo;锛?锛?rdquo;锛屼緥濡傦細
> A=matrix(1:12,nrow=3,ncol=4)
> B=matrix(1:12,nrow=4,ncol=3)
> A%*%B
[,1] [,2] [,3]
[1,] 70 158 246
[2,] 80 184 288
[3,] 90 210 330
鑻涓簄×m鐭╅樀锛岃寰楀埌A'B锛屽彲鐢ㄥ嚱鏁?font face="Courier New, Courier, Monospace">crossprod()锛?/font>璇ュ嚱鏁拌绠楃粨鏋滀笌t(A)%*%B鐩稿悓锛屼絾鏄晥鐜囨洿楂樸€備緥濡傦細
> A=matrix(1:12,nrow=4,ncol=3)
> B=matrix(1:12,nrow=4,ncol=3)
> t(A)%*%B
[,1] [,2] [,3]
[1,] 30 70 110
[2,] 70 174 278
[3,] 110 278 446
> crossprod(A,B)
[,1] [,2] [,3]
[1,] 30 70 110
[2,] 70 174 278
[3,] 110 278 446
鐭╅樀Hadamard绉細鑻={aij}m×n, B={bij}m×n, 鍒欑煩闃电殑Hadamard绉畾涔変负锛?br />A鈯橞={aij bij }m×n,R涓璈adamard绉彲浠ョ洿鎺ヨ繍鐢ㄨ繍绠楃“*”渚嬪锛?br />> A=matrix(1:16,4,4)
> A
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
> B=A
> A*B
[,1] [,2] [,3] [,4]
[1,] 1 25 81 169
[2,] 4 36 100 196
[3,] 9 49 121 225
[4,] 16 64 144 256
R涓繖涓や釜杩愮畻绗︾殑鍖哄埆鍖哄姞浠ユ敞鎰忋€?/font>
7 鐭╅樀瀵硅鍏冪礌鐩稿叧杩愮畻
渚嬪瑕佸彇涓€涓柟闃电殑瀵硅鍏冪礌锛?br />> A=matrix(1:16,nrow=4,ncol=4)
> A
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
> diag(A)
[1] 1 6 11 16
瀵逛竴涓悜閲忓簲鐢?font face="Courier New, Courier, Monospace">diag()鍑芥暟灏嗕骇鐢熶互杩欎釜鍚戦噺涓哄瑙掑厓绱犵殑瀵硅鐭╅樀锛屼緥濡傦細
> diag(diag(A))
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 6 0 0
[3,] 0 0 11 0
[4,] 0 0 0 16
瀵逛竴涓鏁存暟z搴旂敤diag()鍑芥暟灏嗕骇鐢熶互z缁村崟浣嶇煩闃碉紝渚嬪锛?br />> diag(3)
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
8 鐭╅樀姹傞€?/strong>
鐭╅樀姹傞€嗗彲鐢ㄥ嚱鏁?font face="Courier New, Courier, Monospace">solve()锛?/font>搴旂敤solve(a, b)杩愮畻缁撴灉鏄В绾挎€ф柟绋嬬粍ax = b锛岃嫢b缂虹渷锛屽垯绯荤粺榛樿涓哄崟浣嶇煩闃碉紝鍥犳鍙敤鍏惰繘琛岀煩闃垫眰閫嗭紝渚嬪锛?br />> a=matrix(rnorm(16),4,4)
> a
[,1] [,2] [,3] [,4]
[1,] 1.6986019 0.5239738 0.2332094 0.3174184
[2,] -0.2010667 1.0913013 -1.2093734 0.8096514
[3,] -0.1797628 -0.7573283 0.2864535 1.3679963
[4,] -0.2217916 -0.3754700 0.1696771 -1.2424030
> solve(a)
[,1] [,2] [,3] [,4]
[1,] 0.9096360 0.54057479 0.7234861 1.3813059
[2,] -0.6464172 -0.91849017 -1.7546836 -2.6957775
[3,] -0.7841661 -1.78780083 -1.5795262 -3.1046207
[4,] -0.0741260 -0.06308603 0.1854137 -0.6607851
> solve (a) %*%a
[,1] [,2] [,3] [,4]
[1,] 1.000000e+00 2.748453e-17 -2.787755e-17 -8.023096e-17
[2,] 1.626303e-19 1.000000e+00 -4.960225e-18 6.977925e-16
[3,] 2.135878e-17 -4.629543e-17 1.000000e+00 6.201636e-17
[4,] 1.866183e-17 1.563962e-17 1.183813e-17 1.000000e+00
9 鐭╅樀鐨勭壒寰佸€间笌鐗瑰緛鍚戦噺
鐭╅樀A鐨勮氨鍒嗚В涓篈=UΛU',鍏朵腑Λ鏄敱A鐨勭壒寰佸€肩粍鎴愮殑瀵硅鐭╅樀锛孶鐨勫垪涓篈鐨勭壒寰佸€煎搴旂殑鐗瑰緛鍚戦噺锛屽湪R涓彲浠ョ敤鍑芥暟eigen()鍑芥暟寰楀埌U鍜?Lambda;锛?br />> args(eigen)
function (x, symmetric, only.values = FALSE, EISPACK = FALSE)
鍏朵腑锛歺涓虹煩闃碉紝symmetric椤规寚瀹氱煩闃祒鏄惁涓哄绉扮煩闃碉紝鑻ヤ笉鎸囧畾锛岀郴缁熷皢鑷姩妫€娴媥鏄惁涓哄绉扮煩闃点€備緥濡傦細
> A=diag(4)+1
> A
[,1] [,2] [,3] [,4]
[1,] 2 1 1 1
[2,] 1 2 1 1
[3,] 1 1 2 1
[4,] 1 1 1 2
> A.eigen=eigen(A,symmetric=T)
> A.eigen
$values
[1] 5 1 1 1
$vectors
[,1] [,2] [,3] [,4]
[1,] 0.5 0.8660254 0.000000e+00 0.0000000
[2,] 0.5 -0.2886751 -6.408849e-17 0.8164966
[3,] 0.5 -0.2886751 -7.071068e-01 -0.4082483
[4,] 0.5 -0.2886751 7.071068e-01 -0.4082483
> A.eigen$vectors%*%diag(A.eigen$values)%*%t(A.eigen$vectors)
[,1] [,2] [,3] [,4]
[1,] 2 1 1 1
[2,] 1 2 1 1
[3,] 1 1 2 1
[4,] 1 1 1 2
> t(A.eigen$vectors)%*%A.eigen$vectors
[,1] [,2] [,3] [,4]
[1,] 1.000000e+00 4.377466e-17 1.626303e-17 -5.095750e-18
[2,] 4.377466e-17 1.000000e+00 -1.694066e-18 6.349359e-18
[3,] 1.626303e-17 -1.694066e-18 1.000000e+00 -1.088268e-16
[4,] -5.095750e-18 6.349359e-18 -1.088268e-16 1.000000e+00
10 鐭╅樀鐨凜holeskey鍒嗚В
瀵逛簬姝e畾鐭╅樀A锛屽彲瀵瑰叾杩涜Choleskey鍒嗚В锛屽嵆锛欰=P'P锛屽叾涓璓涓轰笂涓夎鐭╅樀锛屽湪R涓彲浠ョ敤鍑芥暟chol()杩涜Choleskey鍒嗚В锛屼緥濡傦細
> A
[,1] [,2] [,3] [,4]
[1,] 2 1 1 1
[2,] 1 2 1 1
[3,] 1 1 2 1
[4,] 1 1 1 2
> chol(A)
[,1] [,2] [,3] [,4]
[1,] 1.414214 0.7071068 0.7071068 0.7071068
[2,] 0.000000 1.2247449 0.4082483 0.4082483
[3,] 0.000000 0.0000000 1.1547005 0.2886751
[4,] 0.000000 0.0000000 0.0000000 1.1180340
> t(chol(A))%*%chol(A)
[,1] [,2] [,3] [,4]
[1,] 2 1 1 1
[2,] 1 2 1 1
[3,] 1 1 2 1
[4,] 1 1 1 2
> crossprod(chol(A),chol(A))
[,1] [,2] [,3] [,4]
[1,] 2 1 1 1
[2,] 1 2 1 1
[3,] 1 1 2 1
[4,] 1 1 1 2
鑻ョ煩闃典负瀵圭О姝e畾鐭╅樀锛屽彲浠ュ埄鐢–holeskey鍒嗚В姹傝鍒楀紡鐨勫€硷紝濡傦細
> prod(diag(chol(A))^2)
[1] 5
> det(A)
[1] 5
鑻ョ煩闃典负瀵圭О姝e畾鐭╅樀锛屽彲浠ュ埄鐢–holeskey鍒嗚В姹傜煩闃电殑閫嗭紝杩欐椂鐢ㄥ嚱鏁?font face="Courier New, Courier, Monospace">chol2inv()锛?/font>杩欑鐢ㄦ硶鏇存湁鏁堛€傚锛?br />> chol2inv(chol(A))
[,1] [,2] [,3] [,4]
[1,] 0.8 -0.2 -0.2 -0.2
[2,] -0.2 0.8 -0.2 -0.2
[3,] -0.2 -0.2 0.8 -0.2
[4,] -0.2 -0.2 -0.2 0.8
> solve(A)
[,1] [,2] [,3] [,4]
[1,] 0.8 -0.2 -0.2 -0.2
[2,] -0.2 0.8 -0.2 -0.2
[3,] -0.2 -0.2 0.8 -0.2
[4,] -0.2 -0.2 -0.2 0.8
11 鐭╅樀濂囧紓鍊煎垎瑙?/strong>
A涓簃×n鐭╅樀锛?font face="Courier New, Courier, Monospace">rank(A)= r, 鍙互鍒嗚В涓猴細A=UDV',鍏朵腑U'U=V'V=I銆傚湪R涓彲浠ョ敤鍑芥暟scd()杩涜濂囧紓鍊煎垎瑙o紝渚嬪锛?br />> A=matrix(1:18,3,6)
> A
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 4 7 10 13 16
[2,] 2 5 8 11 14 17
[3,] 3 6 9 12 15 18
> svd(A)
$d
[1] 4.589453e+01 1.640705e+00 3.627301e-16
$u
[,1] [,2] [,3]
[1,] -0.5290354 0.74394551 0.4082483
[2,] -0.5760715 0.03840487 -0.8164966
[3,] -0.6231077 -0.66713577 0.4082483
$v
[,1] [,2] [,3]
[1,] -0.07736219 -0.7196003 -0.18918124
[2,] -0.19033085 -0.5089325 0.42405898
[3,] -0.30329950 -0.2982646 -0.45330031
[4,] -0.41626816 -0.0875968 -0.01637004
[5,] -0.52923682 0.1230711 0.64231130
[6,] -0.64220548 0.3337389 -0.40751869
> A.svd=svd(A)
> A.svd$u%*%diag(A.svd$d)%*%t(A.svd$v)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 4 7 10 13 16
[2,] 2 5 8 11 14 17
[3,] 3 6 9 12 15 18
> t(A.svd$u)%*%A.svd$u
[,1] [,2] [,3]
[1,] 1.000000e+00 -1.169312e-16 -3.016793e-17
[2,] -1.169312e-16 1.000000e+00 -3.678156e-17
[3,] -3.016793e-17 -3.678156e-17 1.000000e+00
> t(A.svd$v)%*%A.svd$v
[,1] [,2] [,3]
[1,] 1.000000e+00 8.248068e-17 -3.903128e-18
[2,] 8.248068e-17 1.000000e+00 -2.103352e-17
[3,] -3.903128e-18 -2.103352e-17 1.000000e+00
12 鐭╅樀QR鍒嗚В
A涓簃×n鐭╅樀鍙互杩涜QR鍒嗚В锛孉=QR锛屽叾涓細Q'Q锛滻锛屽湪R涓彲浠ョ敤鍑芥暟qr()杩涜QR鍒嗚В锛屼緥濡傦細
> A=matrix(1:16,4,4)
> qr(A)
$qr
[,1] [,2] [,3] [,4]
[1,] -5.4772256 -12.7801930 -2.008316e+01 -2.738613e+01
[2,] 0.3651484 -3.2659863 -6.531973e+00 -9.797959e+00
[3,] 0.5477226 -0.3781696 2.641083e-15 2.056562e-15
[4,] 0.7302967 -0.9124744 8.583032e-01 -2.111449e-16
$rank
[1] 2
$qraux
[1] 1.182574e+00 1.156135e+00 1.513143e+00 2.111449e-16
$pivot
[1] 1 2 3 4
attr(,"class")
[1] "qr"
rank椤硅繑鍥炵煩闃电殑绉╋紝qr椤瑰寘鍚簡鐭╅樀Q鍜孯鐨勪俊鎭紝瑕佸緱鍒扮煩闃礠鍜孯锛屽彲浠ョ敤鍑芥暟qr.Q()鍜?font face="Courier New, Courier, Monospace">qr.R()浣滅敤qr()鐨勮繑鍥炵粨鏋滐紝渚嬪锛?br />> qr.R(qr(A))
[,1] [,2] [,3] [,4]
[1,] -5.477226 -12.780193 -2.008316e+01 -2.738613e+01
[2,] 0.000000 -3.265986 -6.531973e+00 -9.797959e+00
[3,] 0.000000 0.000000 2.641083e-15 2.056562e-15
[4,] 0.000000 0.000000 0.000000e+00 -2.111449e-16
> qr.Q(qr(A))
[,1] [,2] [,3] [,4]
[1,] -0.1825742 -8.164966e-01 -0.4000874 -0.37407225
[2,] -0.3651484 -4.082483e-01 0.2546329 0.79697056
[3,] -0.5477226 -8.131516e-19 0.6909965 -0.47172438
[4,] -0.7302967 4.082483e-01 -0.5455419 0.04882607
> qr.Q(qr(A))%*%qr.R(qr(A))
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
> t(qr.Q(qr(A)))%*%qr.Q(qr(A))
[,1] [,2] [,3] [,4]
[1,] 1.000000e+00 -1.457168e-16 -6.760001e-17 -7.659550e-17
[2,] -1.457168e-16 1.000000e+00 -4.269046e-17 7.011739e-17
[3,] -6.760001e-17 -4.269046e-17 1.000000e+00 -1.596437e-16
[4,] -7.659550e-17 7.011739e-17 -1.596437e-16 1.000000e+00
> qr.X(qr(A))
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
13 鐭╅樀骞夸箟閫?Moore-Penrose)
n×m鐭╅樀A+绉颁负m×n鐭╅樀A鐨凪oore-Penrose閫嗭紝濡傛灉瀹冩弧瓒充笅鍒楁潯浠讹細
鈶? A A+A=A锛涒憽A+A A+= A+锛涒憿(A A+)H=A A+锛涒懀(A+A)H= A+A
鍦≧鐨凪ASS鍖呬腑鐨勫嚱鏁?font face="Courier New, Courier, Monospace">ginv()鍙绠楃煩闃礎鐨凪oore-Penrose閫嗭紝渚嬪锛?br />library(“MASS”)
> A
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
> ginv(A)
[,1] [,2] [,3] [,4]
[1,] -0.285 -0.1075 0.07 0.2475
[2,] -0.145 -0.0525 0.04 0.1325
[3,] -0.005 0.0025 0.01 0.0175
[4,] 0.135 0.0575 -0.02 -0.0975
楠岃瘉鎬ц川1锛?br />> A%*%ginv(A)%*%A
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
楠岃瘉鎬ц川2锛?br />> ginv(A)%*%A%*%ginv(A)
[,1] [,2] [,3] [,4]
[1,] -0.285 -0.1075 0.07 0.2475
[2,] -0.145 -0.0525 0.04 0.1325
[3,] -0.005 0.0025 0.01 0.0175
[4,] 0.135 0.0575 -0.02 -0.0975
楠岃瘉鎬ц川3:
> t(A%*%ginv(A))
[,1] [,2] [,3] [,4]
[1,] 0.7 0.4 0.1 -0.2
[2,] 0.4 0.3 0.2 0.1
[3,] 0.1 0.2 0.3 0.4
[4,] -0.2 0.1 0.4 0.7
> A%*%ginv(A)
[,1] [,2] [,3] [,4]
[1,] 0.7 0.4 0.1 -0.2
[2,] 0.4 0.3 0.2 0.1
[3,] 0.1 0.2 0.3 0.4
[4,] -0.2 0.1 0.4 0.7
楠岃瘉鎬ц川4:
> t(ginv(A)%*%A)
[,1] [,2] [,3] [,4]
[1,] 0.7 0.4 0.1 -0.2
[2,] 0.4 0.3 0.2 0.1
[3,] 0.1 0.2 0.3 0.4
[4,] -0.2 0.1 0.4 0.7
> ginv(A)%*%A
[,1] [,2] [,3] [,4]
[1,] 0.7 0.4 0.1 -0.2
[2,] 0.4 0.3 0.2 0.1
[3,] 0.1 0.2 0.3 0.4
[4,] -0.2 0.1 0.4 0.7
14 鐭╅樀Kronecker绉?/strong>
n×m鐭╅樀A涓巋×k鐭╅樀B鐨刱ronecker绉负涓€涓猲h×mk缁寸煩闃碉紝
鍦≧涓璳ronecker绉彲浠ョ敤鍑芥暟kronecker()鏉ヨ绠楋紝渚嬪锛?br />> A=matrix(1:4,2,2)
> B=matrix(rep(1,4),2,2)
> A
[,1] [,2]
[1,] 1 3
[2,] 2 4
> B
[,1] [,2]
[1,] 1 1
[2,] 1 1
> kronecker(A,B)
[,1] [,2] [,3] [,4]
[1,] 1 1 3 3
[2,] 1 1 3 3
[3,] 2 2 4 4
[4,] 2 2 4 4
15 鐭╅樀鐨勭淮鏁?/strong>
鍦≧涓緢瀹规槗寰楀埌涓€涓煩闃电殑缁存暟锛屽嚱鏁?font face="Courier New, Courier, Monospace">dim()灏嗚繑鍥炰竴涓煩闃电殑缁存暟锛?font face="Courier New, Courier, Monospace">nrow()杩斿洖琛屾暟锛?font face="Courier New, Courier, Monospace">ncol()杩斿洖鍒楁暟锛屼緥濡傦細
> A=matrix(1:12,3,4)
> A
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> nrow(A)
[1] 3
> ncol(A)
[1] 4
16 鐭╅樀鐨勮鍜屻€佸垪鍜屻€佽骞冲潎涓庡垪骞冲潎
鍦≧涓緢瀹规槗姹傚緱涓€涓煩闃电殑鍚勮鐨勫拰銆佸钩鍧囨暟涓庡垪鐨勫拰銆佸钩鍧囨暟锛屼緥濡傦細
> A
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> rowSums(A)
[1] 22 26 30
> rowMeans(A)
[1] 5.5 6.5 7.5
> colSums(A)
[1] 6 15 24 33
> colMeans(A)
[1] 2 5 8 11
涓婅堪鍏充簬鐭╅樀琛屽拰鍒楃殑鎿嶄綔锛岃繕鍙互浣跨敤apply()鍑芥暟瀹炵幇銆?br />> args(apply)
function (X, MARGIN, FUN, ...)
鍏朵腑锛歺涓虹煩闃碉紝MARGIN鐢ㄦ潵鎸囧畾鏄琛岃繍绠楄繕鏄鍒楄繍绠楋紝MARGIN锛?琛ㄧず瀵硅杩愮畻锛?font face="Courier New, Courier, Monospace">MARGIN锛?琛ㄧず瀵瑰垪杩愮畻锛?font face="Courier New, Courier, Monospace">FUN鐢ㄦ潵鎸囧畾杩愮畻鍑芥暟, ...鐢ㄦ潵缁欏畾FUN涓渶瑕佺殑鍏跺畠鐨勫弬鏁帮紝渚嬪锛?br />> apply(A,1,sum)
[1] 22 26 30
> apply(A,1,mean)
[1] 5.5 6.5 7.5
> apply(A,2,sum)
[1] 6 15 24 33
> apply(A,2,mean)
[1] 2 5 8 11
apply()鍑芥暟鍔熻兘寮哄ぇ锛屾垜浠彲浠ュ鐭╅樀鐨勮鎴栬€呭垪杩涜鍏跺畠杩愮畻锛屼緥濡傦細
璁$畻姣忎竴鍒楃殑鏂瑰樊
> A=matrix(rnorm(100),20,5)
> apply(A,2,var)
[1] 0.4641787 1.4331070 0.3186012 1.3042711 0.5238485
> apply(A,2,function(x,a)x*a,a=2)
[,1] [,2] [,3] [,4]
[1,] 2 8 14 20
[2,] 4 10 16 22
[3,] 6 12 18 24
娉ㄦ剰锛?font face="Courier New, Courier, Monospace">apply(A,2,function(x,a)x*a,a=2)涓?font face="Courier New, Courier, Monospace">A*2鏁堟灉鐩稿悓锛屾澶勬棬鍦ㄨ鏄庡浣曞簲鐢?font face="Courier New, Courier, Monospace">alpply鍑芥暟銆?/font>
17 鐭╅樀X'X鐨勯€?/strong>
鍦ㄧ粺璁¤绠椾腑锛屾垜浠父甯搁渶瑕佽绠楄繖鏍风煩闃电殑閫嗭紝濡侽LS浼拌涓眰绯绘暟鐭╅樀銆俁涓殑鍖?ldquo;strucchange”鎻愪緵浜嗘湁鏁堢殑璁$畻鏂规硶銆?br /> > args(solveCrossprod)
function (X, method = c("qr", "chol", "solve"))
鍏朵腑锛?font face="Courier New, Courier, Monospace">method鎸囧畾姹傞€嗘柟娉曪紝閫夌敤“qr”鏁堢巼鏈€楂橈紝閫夌敤“chol”绮惧害鏈€楂橈紝閫夌敤“slove”涓巗love(crossprod(x,x))鏁堟灉鐩稿悓锛屼緥濡傦細
> A=matrix(rnorm(16),4,4)
> solveCrossprod(A,method="qr")
[,1] [,2] [,3] [,4]
[1,] 0.6132102 -0.1543924 -0.2900796 0.2054730
[2,] -0.1543924 0.4779277 0.1859490 -0.2097302
[3,] -0.2900796 0.1859490 0.6931232 -0.3162961
[4,] 0.2054730 -0.2097302 -0.3162961 0.3447627
> solveCrossprod(A,method="chol")
[,1] [,2] [,3] [,4]
[1,] 0.6132102 -0.1543924 -0.2900796 0.2054730
[2,] -0.1543924 0.4779277 0.1859490 -0.2097302
[3,] -0.2900796 0.1859490 0.6931232 -0.3162961
[4,] 0.2054730 -0.2097302 -0.3162961 0.3447627
> solveCrossprod(A,method="solve")
[,1] [,2] [,3] [,4]
[1,] 0.6132102 -0.1543924 -0.2900796 0.2054730
[2,] -0.1543924 0.4779277 0.1859490 -0.2097302
[3,] -0.2900796 0.1859490 0.6931232 -0.3162961
[4,] 0.2054730 -0.2097302 -0.3162961 0.3447627
> solve(crossprod(A,A))
[,1] [,2] [,3] [,4]
[1,] 0.6132102 -0.1543924 -0.2900796 0.2054730
[2,] -0.1543924 0.4779277 0.1859490 -0.2097302
[3,] -0.2900796 0.1859490 0.6931232 -0.3162961
[4,] 0.2054730 -0.2097302 -0.3162961 0.3447627
18 鍙栫煩闃电殑涓娿€佷笅涓夎閮ㄥ垎
鍦≧涓紝鎴戜滑鍙互寰堟柟渚跨殑鍙栧埌涓€涓煩闃电殑涓娿€佷笅涓夎閮ㄥ垎鐨勫厓绱狅紝鍑芥暟lower.tri()鍜屽嚱鏁?font face="Courier New, Courier, Monospace">upper.tri()鎻愪緵浜嗘湁鏁堢殑鏂规硶銆?br /> > args(lower.tri)
function (x, diag = FALSE)
鍑芥暟灏嗚繑鍥炰竴涓€昏緫鍊肩煩闃碉紝鍏朵腑涓嬩笁瑙掗儴鍒嗕负鐪燂紝涓婁笁瑙掗儴鍒嗕负鍋囷紝閫夐」diag涓虹湡鏃跺寘鍚瑙掑厓绱狅紝涓哄亣鏃朵笉鍖呭惈瀵硅鍏冪礌銆?font face="Courier New, Courier, Monospace">upper.tri()鐨勬晥鏋滀笌涔嬪瓚鐒剁浉鍙嶃€備緥濡傦細
> A
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
> lower.tri(A)
[,1] [,2] [,3] [,4]
[1,] FALSE FALSE FALSE FALSE
[2,] TRUE FALSE FALSE FALSE
[3,] TRUE TRUE FALSE FALSE
[4,] TRUE TRUE TRUE FALSE
> lower.tri(A,diag=T)
[,1] [,2] [,3] [,4]
[1,] TRUE FALSE FALSE FALSE
[2,] TRUE TRUE FALSE FALSE
[3,] TRUE TRUE TRUE FALSE
[4,] TRUE TRUE TRUE TRUE
> upper.tri(A)
[,1] [,2] [,3] [,4]
[1,] FALSE TRUE TRUE TRUE
[2,] FALSE FALSE TRUE TRUE
[3,] FALSE FALSE FALSE TRUE
[4,] FALSE FALSE FALSE FALSE
> upper.tri(A,diag=T)
[,1] [,2] [,3] [,4]
[1,] TRUE TRUE TRUE TRUE
[2,] FALSE TRUE TRUE TRUE
[3,] FALSE FALSE TRUE TRUE
[4,] FALSE FALSE FALSE TRUE
> A[lower.tri(A)]=0
> A
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 0 6 10 14
[3,] 0 0 11 15
[4,] 0 0 0 16
> A[upper.tri(A)]=0
> A
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 2 6 0 0
[3,] 3 7 11 0
[4,] 4 8 12 16
19 backsolve&fowardsolve鍑芥暟
杩欎袱涓嚱鏁扮敤浜庤В鐗规畩绾挎€ф柟绋嬬粍锛屽叾鐗规畩涔嬪鍦ㄤ簬绯绘暟鐭╅樀涓轰笂鎴栦笅涓夎銆?br />> args(backsolve)
function (r, x, k = ncol(r), upper.tri = TRUE, transpose = FALSE)
> args(forwardsolve)
function (l, x, k = ncol(l), upper.tri = FALSE, transpose = FALSE)
鍏朵腑锛歳鎴栬€卨涓簄×n缁翠笁瑙掔煩闃碉紝x涓簄×1缁村悜閲忥紝瀵圭粰瀹氫笉鍚岀殑upper.tri鍜?font face="Courier New, Courier, Monospace">transpose鐨勫€硷紝鏂圭▼鐨勫舰寮忎笉鍚?br />瀵逛簬鍑芥暟backsolve()鑰岃█锛?br />渚嬪锛?br /> > A=matrix(1:9,3,3)
> A
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> x=c(1,2,3)
> x
[1] 1 2 3
> B=A
> B[upper.tri(B)]=0
> B
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 2 5 0
[3,] 3 6 9
> C=A
> C[lower.tri(C)]=0
> C
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 0 5 8
[3,] 0 0 9
> backsolve(A,x,upper.tri=T,transpose=T)
[1] 1.00000000 -0.40000000 -0.08888889
> solve(t(C),x)
[1] 1.00000000 -0.40000000 -0.08888889
> backsolve(A,x,upper.tri=T,transpose=F)
[1] -0.8000000 -0.1333333 0.3333333
> solve(C,x)
[1] -0.8000000 -0.1333333 0.3333333
> backsolve(A,x,upper.tri=F,transpose=T)
[1] 1.111307e-17 2.220446e-17 3.333333e-01
> solve(t(B),x)
[1] 1.110223e-17 2.220446e-17 3.333333e-01
> backsolve(A,x,upper.tri=F,transpose=F)
[1] 1 0 0
> solve(B,x)
[1] 1.000000e+00 -1.540744e-33 -1.850372e-17
瀵逛簬鍑芥暟forwardsolve()鑰岃█锛?br />渚嬪锛?br /> > A
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> B
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 2 5 0
[3,] 3 6 9
> C
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 0 5 8
[3,] 0 0 9
> x
[1] 1 2 3
> forwardsolve(A,x,upper.tri=T,transpose=T)
[1] 1.00000000 -0.40000000 -0.08888889
> solve(t(C),x)
[1] 1.00000000 -0.40000000 -0.08888889
> forwardsolve(A,x,upper.tri=T,transpose=F)
[1] -0.8000000 -0.1333333 0.3333333
> solve(C,x)
[1] -0.8000000 -0.1333333 0.3333333
> forwardsolve(A,x,upper.tri=F,transpose=T)
[1] 1.111307e-17 2.220446e-17 3.333333e-01
> solve(t(B),x)
[1] 1.110223e-17 2.220446e-17 3.333333e-01
> forwardsolve(A,x,upper.tri=F,transpose=F)
[1] 1 0 0
> solve(B,x)
[1] 1.000000e+00 -1.540744e-33 -1.850372e-17
20 row()涓巆ol()鍑芥暟
鍦≧涓畾涔変簡鐨勮繖涓や釜鍑芥暟鐢ㄤ簬鍙栫煩闃靛厓绱犵殑琛屾垨鍒椾笅鏍囩煩闃碉紝渚嬪鐭╅樀A={aij}m×n锛?br />row()鍑芥暟灏嗚繑鍥炰竴涓笌鐭╅樀A鏈夌浉鍚岀淮鏁扮殑鐭╅樀锛岃鐭╅樀鐨勭i琛岀j鍒楀厓绱犱负i锛屽嚱鏁癱ol()绫讳技銆備緥濡傦細
> x=matrix(1:12,3,4)
> row(x)
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 2 2 2 2
[3,] 3 3 3 3
> col(x)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 1 2 3 4
[3,] 1 2 3 4
杩欎袱涓嚱鏁板悓鏍峰彲浠ョ敤浜庡彇涓€涓煩闃电殑涓婁笅涓夎鐭╅樀锛屼緥濡傦細
> x
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> x[row(x)<col(x)]=0
> x
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 2 5 0 0
[3,] 3 6 9 0
> x=matrix(1:12,3,4)
> x[row(x)>col(x)]=0
> x
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 0 5 8 11
[3,] 0 0 9 12
21 琛屽垪寮忕殑鍊?/strong>
鍦≧涓紝鍑芥暟det(x)灏嗚绠楁柟闃祒鐨勮鍒楀紡鐨勫€硷紝渚嬪锛?br />> x=matrix(rnorm(16),4,4)
> x
[,1] [,2] [,3] [,4]
[1,] -1.0736375 0.2809563 -1.5796854 0.51810378
[2,] -1.6229898 -0.4175977 1.2038194 -0.06394986
[3,] -0.3989073 -0.8368334 -0.6374909 -0.23657088
[4,] 1.9413061 0.8338065 -1.5877162 -1.30568465
> det(x)
[1] 5.717667
22鍚戦噺鍖栫畻瀛?/strong>
鍦≧涓彲浠ュ緢瀹规槗鐨勫疄鐜板悜閲忓寲绠楀瓙锛屼緥濡傦細
vec<-function (x){
t(t(as.vector(x)))
}
vech<-function (x){
t(x[lower.tri(x,diag=T)])
}
> x=matrix(1:12,3,4)
> x
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> vec(x)
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
[7,] 7
[8,] 8
[9,] 9
[10,] 10
[11,] 11
[12,] 12
> vech(x)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 2 3 5 6 9
23 鏃堕棿搴忓垪鐨勬粸鍚庡€?/strong>
鍦ㄦ椂闂村簭鍒楀垎鏋愪腑锛屾垜浠父甯歌鐢ㄥ埌涓€涓簭鍒楃殑婊炲悗搴忓垪锛孯涓殑鍖?ldquo;fMultivar”涓殑鍑芥暟tslag()鎻愪緵浜嗚繖涓姛鑳姐€?br /> > args(tslag)
function (x, k = 1, trim = FALSE)
鍏朵腑锛歺涓轰竴涓悜閲忥紝k鎸囧畾婊炲悗闃舵暟锛屽彲浠ユ槸涓€涓嚜鐒舵暟鍒楋紝鑻rim涓哄亣锛屽垯杩斿洖搴忓垪涓庡師搴忓垪闀垮害鐩稿悓锛屼絾鍚湁NA鍊硷紱鑻rim椤逛负鐪燂紝鍒欒繑鍥炲簭鍒椾腑涓嶅惈鏈塏A鍊硷紝渚嬪锛?br />> x=1:20
> tslag(x,1:4,trim=F)
[,1] [,2] [,3] [,4]
[1,] NA NA NA NA
[2,] 1 NA NA NA
[3,] 2 1 NA NA
[4,] 3 2 1 NA
[5,] 4 3 2 1
[6,] 5 4 3 2
[7,] 6 5 4 3
[8,] 7 6 5 4
[9,] 8 7 6 5
[10,] 9 8 7 6
[11,] 10 9 8 7
[12,] 11 10 9 8
[13,] 12 11 10 9
[14,] 13 12 11 10
[15,] 14 13 12 11
[16,] 15 14 13 12
[17,] 16 15 14 13
[18,] 17 16 15 14
[19,] 18 17 16 15
[20,] 19 18 17 16
> tslag(x,1:4,trim=T)
[,1] [,2] [,3] [,4]
[1,] 4 3 2 1
[2,] 5 4 3 2
[3,] 6 5 4 3
[4,] 7 6 5 4
[5,] 8 7 6 5
[6,] 9 8 7 6
[7,] 10 9 8 7
[8,] 11 10 9 8
[9,] 12 11 10 9
[10,] 13 12 11 10
[11,] 14 13 12 11
[12,] 15 14 13 12
[13,] 16 15 14 13
[14,] 17 16 15 14
[15,] 18 17 16 15
[16,] 19 18 17 16