TA的每日心情 | 慵懒 2017-7-12 08:29 |
|---|
签到天数: 7 天 连续签到: 2 天 [LV.3]偶尔看看II 累计签到:7 天 连续签到:2 天
|
发表于 2009-8-7 23:50:24
|
显示全部楼层
本帖最后由 晓夜寒风 于 2009-8-7 23:56 编辑
$ m% f/ y/ ?' ^! n
1 H+ n) t1 i6 l+ XIf the global variable does not exist the first time you issue the global statement, it is initialized to the empty matrix.
9 R0 p8 Q. m, T+ C' |: C即 假设定义x 为全局变量
& c: l: p0 X* d- Z( M/ E# ? 则 使用 global x
& V1 b R- |/ a h编译的时候 解释器第一次遇到 ”global x ” 就为其分配空间,
6 @2 f( G& F" p! r 以后再从其他文件里遇到 x,就使其值与之前的x 相同。; ~9 _8 F( }- `) a! u: w6 v' d
4 `! \1 \5 H9 q: P7 v6 k
5 P ]% I" q; H# l; [举例
9 u7 e! e* R; F/ C5 K+ [$ b1 P% M9 N t( C. G# Q S' x( j
注意 文件一 与文件二 共享了 TICTOC 这个变量,在文件三中实现了时间长度计算!
$ q6 W% k2 E# K5 \- }
* [' M( s8 `( A: N4 G8 s. ~! g7 T+ z文件一
( R2 `7 q$ }3 q0 I0 K& P5 ], s3 r
2 ]8 y# L7 z3 V% q* M' r/ L- function ticjh. h1 z5 J$ C' o3 f$ @" M- J. Z
- % TIC Start a stopwatch timer.( y$ \- t* e* i- k. l; T
- % TIC; any stuff; TOC( D) s$ U! x$ {, _9 U
- % prints the time required.5 |0 p% x5 p! p( ?1 l
- % See also: TOC, CLOCK.
& }9 W2 @6 S0 i& s* X - global TICTOC T( N. ]/ d- _) y4 @
- TICTOC = clock;
) V7 m( h! F$ W$ {' q
复制代码 文件二
2 @) `$ `( [& C- function t = tocjh2 R$ Z. r, `8 S( x) v% g
- % TOC Read the stopwatch timer.4 P: t0 ~1 N! ^% O$ {
- % TOC prints the elapsed time since TIC was used.
( T& d3 ~8 F8 B- ?' P- L2 {; f p - % t = TOC; saves elapsed time in t, does not print.
7 {1 n* ]! s& o' ] - % See also: TIC, ETIME.
" E' ~) p( U e9 `8 |/ { - global TICTOC
8 j1 }6 y* d" h5 y' B% S - if nargout < 1$ l: ~3 g6 a; Z
- elapsed_time = etime(clock, TICTOC)- \# Q- k( ?- w% q" g
- else
2 i2 e8 a; d- J8 _ - t = etime(clock, TICTOC);/ X; ?- G+ l0 w6 }3 w& B, z- y
- end% E% P- Y) r$ A5 b; s9 P, ~
复制代码 文件三
2 x7 \4 z) q/ P K: C, U# ~: ^- ticjh;
' C2 ~/ R3 W+ z: S - for i=1:1:100
~, W8 t9 g+ N - for j=1:1:1001 O& f7 I' d, u" |
- ( _& F0 o; {* J& f
- end( v6 x4 \3 k2 h9 c
- end0 ^" u" R0 M7 j( J/ w, n# w
- t=tocjh;
2 u9 Q3 q- U+ J* X' M& Z, `
复制代码 PSAT 中应用了很多global。 |
|