* n6 E3 j Y; p4 c5 S% r) x; r9 ^: m
下面逐个讲解:# N$ z' h9 C: |0 b0 p" Q, L
/ E+ s- ?+ S( S! fC = cov(A) y; H V8 C. H 0 ~ G' C4 ?5 oC = cov(A) returns the covariance.) b) P) V" a3 D7 N2 l; w4 e/ a( B
( p' o- S. ~- S* y) O4 D2 wC = cov(A)返回协方差。$ S B8 D7 j& K/ Q6 l3 m' {5 M2 [
) T4 `9 f& \! C# X
If A is a vector of observations, C is the scalar-valued variance.% X- d) T9 `5 L: _3 f k
- u! m- V- E- e5 Z. r6 I
如果A是一个观测向量,那么C是一个标量值的方差。2 _/ n$ R Z7 z% N* ^2 U1 g
( i$ l8 g" p5 S! F) z) w m6 l
If A is a matrix whose columns represent random variables and whose rows represent observations, C is the covariance matrix with the corresponding column variances along the diagonal.9 N1 x5 V0 X& D, z& V6 p
g$ |8 E) [5 b
如果A是矩阵,其列表示随机变量,其行表示观测值,则C是协方差矩阵,沿对角线具有相应的列方差。(协方差矩阵的协方差是列的协方差值) 4 |% N2 c r4 j" M6 a
7 o5 |" J ~$ V+ D
C is normalized by the number of observations-1. If there is only one observation, it is normalized by 1. 5 `* w2 `5 R1 ^
* D: j G% e% ~( D, l
C由观察数-1归一化。 如果只有一个观察值,则将其标准化为1。& r$ W7 l9 z% g6 j9 d5 m
7 T: g! B/ w$ D! B: u7 [4 R
If A is a scalar, cov(A) returns 0. If A is an empty array, cov(A)returns NaN.0 n- t# e; g9 \0 l
9 D a) G: F( Q- x6 h7 P4 d
如果A是标量,则cov(A)返回0.如果A是空数组,则cov(A)返回NaN。7 _3 T3 h* f5 Y6 a: A4 b+ B
1 c0 s. U$ ~8 H# Y' H$ ~4 a! s: M7 G . ?- [1 \/ D: w; v0 ZC = cov(A,B) 1 y" r$ d3 s4 B3 _4 [, ?
; ?8 C9 I7 u' M, w3 u G0 ]5 O
C = cov(A,B) returns the covariance between two random variables A and B. - x1 J' A) j# ] O R3 M% m' M- p3 Q% K$ c4 e- A" y3 F/ ?, T' r( L+ L
C = cov(A,B) 返回两个随机变量A和B之间协方差。/ \3 Q3 \, }* i4 [
) l2 m8 V: F) J. M/ K; L9 G
If A and B are vectors of observations with equal length, cov(A,B) is the 2-by-2 covariance matrix. * Q- E4 [2 i, g' d& |& x0 V/ O7 O$ O$ q' R
; P0 }; R2 g, g9 `8 K) Q0 F
如果A和B是同等长度的观测向量,那么C是一个2*2的协方差矩阵。 / ?( o/ f _! f5 r7 z
$ l- n% I9 t+ X0 j* D: f
If A and B are matrices of observations, cov(A,B) treats A and B as vectors and is equivalent to cov(A(,B(). A and B must have equal size.8 N/ R3 x! G) ` s/ \- Q8 n
" ]& b+ W% n; I
如果A和B是观察矩阵,则cov(A,B)将A和B视为向量,并且等同于cov(A(,B(:))。 A和B必须具有相同的大小。* t( I& k, w% C: B
, V W0 a/ z! ?9 [0 B2 [3 f
If A and B are scalars, cov(A,B) returns a 2-by-2 block of zeros. If A and B are empty arrays, cov(A,B) returns a 2-by-2 block of NaN.$ g/ i0 W2 O$ |$ n; V8 ~- m2 M
# F( X5 x2 S9 i! g
如果A和B是标量,则cov(A,B)返回2乘2的零块。 如果A和B是空数组,则cov(A,B)返回2乘2的NaN块。 ) v7 q- }- _, h* f. ^
" s+ l% K7 G& U
6 |" q7 f; K: @( x+ C/ B2 ], @/ f$ p+ r9 A0 s5 T. c/ }$ D C = cov(___,w)9 a" P% M9 H i
3 \! w# H0 N9 p$ a: d% }
C = cov(___,w) specifies the normalization weight for any of the previous syntaxes. When w = 0 (default), C is normalized by the number of observations-1. When w = 1, it is normalized by the number of observations.& I- p& O+ j2 n2 r0 U4 f A4 X
) h8 w5 m1 K- v' t2 w
C = cov(___,w)指定任何先前语法的归一化权重。 当w = 0(默认值)时,C由观测数-1归一化。 当w = 1时,它通过观察次数归一化。 ) P5 r- {2 Z. A$ A4 Y6 m, r 5 j% O6 k1 f1 c* Y3 F. Z$ B0 Z& t4 [ C = cov(___,nanflag)3 M: w8 n5 \1 p% L
* h$ A! h# H( l4 a( uC = cov(___,nanflag) specifies a condition for omitting NaN values from the calculation for any of the previous syntaxes. For example, cov(A,'omitrows') will omit any rows of A with one or more NaN elements.! t# ?, q$ L, Y' A+ ?
+ k& Z% p9 L; S' h0 J: WC = cov(___,nanflag)指定从任何先前语法的计算中省略NaN值的条件。 例如,cov(A,'omitrows')将省略具有一个或多个NaN元素的A的任何行。$ } C$ }6 X: ^ ]' N' x
3 d; k& x! C6 \1 C$ l ) V6 ~& K X H8 G( R示例 4 L& V L6 x# q" I7 W 2 u. b0 o1 y% _# K# E下面举例说明重要的语法格式:5 C/ z+ P$ u3 ^! a6 J
0 N6 x- s8 _# o1 r& m3 P$ lC = cov(A) 举例(矩阵的协方差) n x# L7 V ^" Z. }7 \- G8 u8 J
2 l3 H7 ]7 h3 } L
Create a 3-by-4 matrix and compute its covariance( Q* |' J4 R( z3 h( e( x' I
) a5 y$ F5 K# [5 J @6 l. q
A = [5 0 3 7; 1 -5 7 3; 4 9 8 10];
C = cov(A)* R6 K6 \9 R' G5 \5 _
v4 P/ c: j, ~
% S. d5 K% s" e, B6 `9 w/ w. Y! b: J) n, B4 n0 k( P$ x
) R& L! Z7 D+ ~" c- o
C = 4×4
/ l8 A" L& \, I6 x0 I& m8 n
4.3333 8.8333 -3.0000 5.6667
8.8333 50.3333 6.5000 24.1667
-3.0000 6.5000 7.0000 1.0000
5.6667 24.1667 1.0000 12.3333
% t" Y& Q: ~ a( F7 K
5 n! h+ ], J# o3 R( ^( ] _
! n8 B( v# B# X* i( e' h: [3 h1 g- k
% g, ^2 }; F' g2 Q' G" X
7 \# v+ b2 p6 I) n" U
Since the number of columns of A is 4, the result is a 4-by-4 matrix. 1 p3 A6 d" w N% O" P" H & N4 m7 _" M% H/ f由于矩阵A有4列,表示有4个随机变量,那么协方差矩阵是4*4的。 % ~; P3 v2 Y D: ]$ @+ Y U* q+ j. F* g- P' o- [9 L
" n# j3 L/ y b; }0 a- B5 W) x cov(A,B) 举例之两个向量之间的协方差) B# K7 O6 T( q4 `7 @+ z
! Z: e1 j/ S& I- H# n; y$ z1. Create two vectors and compute their 2-by-2 covariance matrix. ( T) @" J' d4 L. V( [7 D 5 x- q9 g7 u S; b" E9 Q
A = [3 6 4];
B = [7 12 -9];
cov(A,B) m: _# R- V R, g7 y! }- C
: n9 _ k: g$ F' O 8 Z) o% p' @8 `% \2 t8 m4 E. ~8 f6 ~) I9 }! F