Академический Документы
Профессиональный Документы
Культура Документы
.., ..
2005
MATLAB
(desktop) MATLAB
4
5
MATLAB
(Getting Help)
MATLAB
7
13
13
Array Editor
MATLAB
16
18
20
()
23
MATLAB
24
(Colon)
28
30
35
37
41
50
65
66
MATLAB-
(Flow Control)
1. MATLAB
2.
(General purpose commands)
74
75
86
97
108
114
121
123
3.
126
4.
(Elementary math functions).
127
5.
(Elementary matrices and matrix manipulation)
6. -
(Matrix functions - numerical linear algebra)
7.
(Interpolation and polynomials)
129
132
8.
(Data analysis and Fourier transforms)
9.
(Character strings)
133
135
MATLAB
137
MATLAB
MATLAB ( MATrix LABoratory - )
The MathWorks, Inc. (, ., . )
,
.
, Fortran, C C++.
MATLAB:
, ;
, ;
,
.
MATLAB , ,
.
, - ,
, Fortran
C. ,
,
REDUCE, MACSYMA, DERIVE, Maple, Mathematica, Theorist,
.
MATLAB .
, MATLAB
.
, -.
.
(Application Toolboxes Toolboxes),
-
.
Toolboxes , ; , , ,
, -,
, ,
,
SIMULINK.
MATLAB Application Toolboxes,
MATLAB,
.
MATLAB
MATLAB-. MATLAB
Windows. MATLAB- $matlabroot\work, $matlabroot , MATLAB.
, MATLAB - (master M-file)
matlabrc.m., startup.m ( ). matlabrc.m,
local, The MathWorks, . startup.m
()
. (. ),
, ..
startup.m $matlabroot\toolbox\local (
MATLAB-).
MATLAB-. MATLAB Exit
MATLAB ( MATLAB) File MATLAB-, -
quit () Command Window.
, MATLAB finish.m, (. ) , -
MATLAB. finish.m . ,
MATLAB, ,
, . $matlabroot\toolbox\local , finish.m:
finishsav.m save, MATLAB-.
finishdlg.m , .
(desktop) MATLAB
MATLAB ,
:
Command Window ( )
MATLAB.
Command History ( ) ,
Command Window, .
Launch Pad ( )
MATLAB .
Current Directory Browser ( )
MATLAB, ,
, .
Help Browser ( )
MATLAB.
Workspace Browser ( )
(workspace) MATLAB.
Array Editor ( ) ,
.
Editor/Debugger (/) ,
-, .. , MATLAB.
MATLAB (.1).
(. . 1). ,
, Dock View .
.
(Title Bar) .
MATLAB
Default ( ) Desktop Layout ( )
View () . MATLAB
(context menu),
(), .
. MATLAB
, , .
(Workspace)
. 1. MATLAB
MATLAB
MATLAB
.
-
Simulink
Undo
Redo
(Help)
Undo ; Redo .
. 2. (Toolbar)
Command Window ( )
1.
(>>) Command Window , MATLAB
. >> . , 33
A = [1 2 3; 4 5 6; 7 8 10]
Enter ( Return) , MATLAB
A=
1 2 3
4 5 6
7 8 10.
() (workspace) MATLAB.
! MATLAB , .. MATLAB ! ()
- !
Enter. MATLAB . ,
magic(2)
ans =
1
3
2.
, Shift+Enter .
Enter .
K>> , MATLAB
(debug mode). .
2. (Evaluating a Selection)
Evaluate Selection. MATLAB , , -.
3. (Opening a Selection)
-
Open Selection. -
/ (Editor/Debugger).
! MATLAB- .
MATLAB ,
!
4.
,
( ; ). ,
format short;
x = (1:10)';
logs = [x log10(x)]
Enter
1 10.
5.
, , (...)
, ; Enter
, . ,
s = 1 1/2 + 1/3 1/4 + 1/5 1/6 + 1/7 ...
1/8 + 1/9 1/10 + 1/11 1/12;
, , . ,
headers = ['Author Last Name, Author First Name, ...
'Author Middle Initial]
headers =
. , , , . ,
rho = (1+ sqt(5))/2
Enter MATLAB :
Undefined function or variable 'sqt'.
( sqt),
sqrt ( ).
, .
.
r. . . (smart recall) , ,
. , plo
, plo.
.
,
Ctrl+
Ctrl+
Home
End
Esc
Delete
Backspace
Shift+home
Shift+end
Ctrl+p
Ctrl+n
Ctrl+b
Ctrl+f
Ctrl+r
Ctrl+l
Ctrl+a
Ctrl+e
Ctrl+u
Ctrl+d
Ctrl+h
Ctrl+k
.
.
.
.
.
.
.
.
.
.
.
.
8.
. .
!
clc . clc, home
MATLAB (>>) ,
.
9.
( ; ), Enter MATLAB (), . . ,
A = magic(100);
Enter MATLAB A 100100,
.
10.
,
( ).
more . more (). more on, MATLAB
( ).
Enter
more off.
11.
, . Preferences File
. ,
MATLAB-.
!
format.
, MATLAB- .
.
x = [4/3 1.2345e6]
short e
1.3333e+000 1.2345e006
short
1.3333 0.0000
+
++
(Help) . sprintf fprintf .
. Preferences File . format compact , . , , format loose.
12.
Print File.
, Print
Selection File.
13.
-. -, .. MATLAB, ,
MATLAB-, ..
- Enter.
- echo, ..
MATLAB -.
.
Ctrl+c Ctrl+Break .
. !
MATLAB- .
MATLAB. , MATLAB.
- /. - ,
, Open Selection . -
/ (Editor/Debugger).
. - ,
Enter.
M- (Editor), , .
. MATLAB
diary ().
,
MATLAB, . . ,
, sept23 (23 ), MATLAB,
diary('sept23.out'). : diary('off')
. Command History ( ) ,
.
(Getting Help)
MATLAB
. , , . ,
: help, helpwin lookfor.
1. HELP. , .
help, , ( )
MATLAB.
() MATLAB- (. 1).
help(topic) help topic , topic (,
matlab\polyfun), .
help fun, fun , ,
-.
2. HELPWIN. , help,
(Help Browser) (. . 1).
3. LOOKFOR. -
. , , lookfor XYZ XYZ
(HELP text) -
MATLAB (MATLABPATH). , lookfor
.
MATLAB
(workspace) MATLAB- ( (array), MATLAB . , -, , . , , :
t = 0 : pi/4 : 2*pi; y = sin(t);
z = 5;
y t,
, z.
.
(Workspace Browser )
(), -
, . :
View MATLAB Workspace.
(Launch Pad) Workspace.
workspace MATLAB-.
( ):
. 3.
. ,
, . Name ( ).
! who . whos
. , who MATLAB :
Your variables are ( ):
A M S v
whos :
Name Size Bytes
Class
A 4x4
128
double array
M
8x1
2368
cell array
S
1x1
398
struct array
v
5x9
90
char array
Grand total is 286 elements using 2984 bytes
,
: double array , cell array
, struct array , char array .
.
MATLAB. MATLAB-,
.
MAT- (MAT-file), () .
MAT-, , ,
.
. :
1. File Save Workspace As,
. Save.
2. (File name). MATLAB
.mat .
3. Save.
MAT-
.
. :
1. . Shift Ctrl.
1. Save Selection As.
.
! save . , save('june10')
june10.mat. , matlab.mat .
, , ascii. , save 'june10' X Y X Y .
. :
1.
, Import Data. Open ().
2. MAT-, Open.
, MAT-,
MATLAB.
! load . , load('june10')
june10.mat.
. MAT- june10 A, B, C, june10 .
,
june10.
. . :
1. ( Shift
Ctrl). Select All Edit
.
2. :
- Delete.
- Delete Edit.
-
- Delete Selection .
3. , Yes.
Clear Workspace Edit
.
! clear . ,
clear A M
A M , clear . , clear all , .. MATLAB
(Base)
(Stack). - MATLAB ()
() ,
MATLAB.
Stack . (
).
.
.
, ,
Graph Selection , .
(figure window).
.
Array Editor
() Array Editor
- , .
. :
1. .
2.
-
Open Selection .
.
:
.4.
. (. .
4) Window.
!
. , ,
m (. . 4) , MATLAB
m=
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15
MATLAB
- , ,
MATLAB (search path). (), MATLAB , -
. ,
MATLAB MathWorks . - , MATLAB, , ,
MATLAB.
! - -, MATLAB, ,
$matlabroot/toolbox/matlab, $matlabroot
MATLAB . - $matlabroot/toolbox/matlab,
MATLAB- .
. MATLAB.
, . (subdirectories)
; ,
. MATLAB
pathdef.m.
. MATLAB (, ..), ,
foo, . foo ,
MATLAB :
1. foo .
2. , foo .
3. foo.m.
4. MATLAB foo.m.
, -, .
,
, . MATLAB , , ; () .
, .. , which FileName, FileName .
.
MATLAB Set Path, (), . Set Path File , pathtool
MATLAB .
,
MATLAB
. 5. Set Path
. 5 :
Add Folder
- ().
- .
- .
- .
- .
- .
- .
- ( ).
MATLAB
. , , () MATLAB. ,
- , pen
.
.
Current Directory ( )
,
:
.
, , .
(...) (. . 2) .
(Current Directory Browser) . , , MATLAB
().
MATLAB. MATLAB-,
Current Directory View, filebrowser (Command Window).
- ( )
. 6.
/. (. . 6) (help)
.
. Current Directory , , .
! dir , dir. what , ,
MATLAB. which FileName, FileName ,
.
MATLAB, , , .. (. ).
.
. /,
.
(). :
1.
(. 6).
(Find & Replace):
2. :
- Find what.
- Look in,
.
- Match case ( ) Whole word (
).
- Subdirectories (),
.
3. Find.
Find & Replace
. , .
4. -
:
- .
- Enter.
- Open .
/,
, .
5. (),
(. .).
. , . :
1. - / MATLAB Editor.
, /.
2. Look in Find & Replace ,
. Replace () .
3. Replace with , .
4. Replace , Replace All,
.
.
5. Save File / .
()
MATLAB load help.
, :
load August17.dat
help magic
type rank.
.
load('August17.dat')
help('magic')
type('rank')
/ MATLAB.
command argument
command('argument')
, . ,
31- -
August1.dat, August2.dat, ..
for d = 1:31
s = ['August' int2str(d) '.dat']
load(s)
end
int2str, ,
,
.
MATLAB
.
, (.. ) MATLAB-.
, , , ,
.. MATLAB.
MATLAB
. , , . MATLAB- , () . 3-by-3,
.
.
A = pascal (3)
A=
1
1
1
1
2
3
1
3
6
, (magic square):
B = magic(3)
B=
8
3
4
1
5
9
6
7
2
32 :
C = fix(10*rand(3,2))
C=
9
4
2
8
6
7.
rand(3,2) 32
0 1, fix
.
- m1 matrix, - 1 n ,
11.
, ,
( ).
u = [3; 1; 4]
v = [2 0 -1]
s=7
- u , - v s (
):
u=
3
1
4
v=
2
0 1
s=
7
, .
,
( MATLAB ( )
). , ( )
Error using ==> +
Matrix dimensions must agree.
- - ( * 8). ( ) ( ).
v u :
x = v*u
x=
2
X = u*v
X=
6 0 -3
2 0 -1
8 0 -4
,
aij aji, . MATLAB () (). A = A. :
X = B'
X=
8 3 4
1 5 9
6 7 2
- - . x y , x*y , x'*y y'*x . ,
: ,
.
, z, z' -
. MATLAB- . . , z z.',
. ,
z = [1+2i 3+4i]
z' =
1-2i
3-4i
z.'
z.' =
1+2i
3+4i
, x'*y y'*x ,
x'*x
.
MATLAB = * . MATLAB
. , :
Error using ==> *
Inner matrix dimensions must agree.
(Subscripts)
, (), 4- :
F = magic(4)
F=
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
i- j- F F (i,j). , F (4,2)
. , F(4,2)
14. , F,
ans =
34
, ,
.
,
F(k). ( ).
MATLAB- (
) , MATLAB -, . , , F (8) 14
F (4,2).
, ,
t = F (4,5)
Index exceeds matrix dimensions
( )
, - ,
, .
X = A;
X(4,5) = 17
X=
16 3
5 0
9 6
4 15
2 13 0
11 8 0
7 12 0
14 1 17
(Colon)
, : , MATLAB-.
. 1:10 -,
1 10:
1 2 3 4 5 6 7 8 9 10
, . ,
100 : -7 : 50
100 93 86 79 72 65 58 51
0 : pi/4 : pi
k j- F. ,
sum(F (1:4, 4))
, , 4- . .
,
end (
- end ). ,
sum(F (:, end))
F . : ans = 34. 4 4 34 ? , 1
16 ( 4 4)
- ,
sum(1:16)/4
, , ans = 34.
, .
, , MATLAB- eye, eye(n)
nxn, eye(m,n) - mxn,
eye(size(A)) , A. ,
I = eye(3)
I=
1
0
0
0
1
0
0
0
1
I = eye (3,5)
I=
1
0
0
0
1
0
0
0
1
0
0
0
I = eye (4,2)
I=
1
0
0
0
0
0
0
1
0
0
, ( ), MATLAB- zeros, zeros (n) nxn, zeros (m,n) mxn, zeros (size(A)) A.
Z = zeros(2,4)
Z=
0 0 0 0
0 0 0 0
, ( ), MATLAB- ones,
ones (n) nxn, ones (m,n)
mxn, ones (size(A)) ,
A.
S = 5*ones(3, 3)
S=
5 5 5
5 5 5
5 5 5
, rand 0 1, ,
randn .
N = fix(10*rand(1,10))
N=
4 9 4 4 8 5 2 6 8 0
R = randn(4,4)
R=
1.0668 0.2944 -0.6918 -1.4410
0.0593 -1.3362 0.8580 0.5711
-0.0956 0.7143 1.2540 -0.3999
-0.8323 1.6236 -1.5937 0.6900
. , . A and B,
X, AX = B XA = B?
.
7x = 21
? , , .
x = 3. .
x = 21/7 = 3
7 (.. 7-1 = 0.142857), 7-1 21.
, 7-1 (),
.
; MATLAB . ,
MATLAB ,
,
. / ( ( - slash)) \ ( (backslash)) ,
:
X = A\B AX = B
X = B/A XA = B.
AX = B
XA = B A. A . X = A\B A B
. X B,
A. X = B/A, . , AX = B , XA = B. , \ , / . , \ ;
/
(B/A)' = (A'\B')
, A . A
mn, :
1. m = n . .
2. m > n . .
3. m < n .
m .
\ . , , :
,
,
,
A
- b , .. Ax = b. x = A\b , b. ,
x = A\u
x=
10
-12
5
. , A*x
u ( ).
A B , X = A\B ,
X = A\B
X=
19 -3 -1
-17 4 13
6
0 -6
, A*X B.
. , pascal(3),
. , .
A , . A , AX = B , . \ , A\B, , A
, .
.
. y t,
t
0.0
0.3
0.8
1.1
0.82
0.72
0.63
0.60
1.6 0.55
2.3 0.50
MATLAB :
t = [0 .3 .8 1.1 1.6 2.3]';
y = [0.82 0.72 0.63 0.60 0.55 0.50]';
.
y(t) = c1 + c2 e-t
, y , , , e-t. c1 c2
, .
, 62
E = [ones(size(t)) exp(-t)]
E=
1.0000 1.0000
1.0000 0.7408
1.0000 0.4493
1.0000 0.3329
1.0000 0.2019
1.0000 0.1003
\ :
c = E\y
c=
0.4760
0.3413
,
y(t) = 0.476 + 0.3413 e-t
( 0.1), .
T = (0 : 0.1 : 2.5)';
Y = [ones(size(T)) exp(-T)]*c;
plot(T, Y, '-', t, y, 'o')
, E*c y, .
A , . A , AX = B . \
, .
. ,
. , \
. . MATLAB , m ( m -
), . ,
.
R = fix (10*rand(2,4))
R=
6 8 7 3
3 5 4 1
b = fix (10*rand(2,1))
b=
1
2
Rx = b .
R ,
rational ( ). :
p = R\b
p=
0
5/7
0
-11/7
p(2), R . p(4)
R ( QR- ).
, AX = I XA = I
X. A,
A-1 inv. ()
,
. , , det
. ,
A = pascal (3)
A=
1 1 1
1 2 3
1 3 6
d = det (A)
X = inv (A)
d=
1
X=
3 -3 1
-3 5 -2
1 -2 1
, A
, . ,
B = magic(3)
B=
8 1 6
3 5 7
4 9 2
d = det(B)
X = inv(B)
d=
-360
X=
0.1472 -0.1444 0.0639
-0.0611 0.0222 0.1056
-0.0194 0.1889 -0.1028
X, rational , , , 360.
A , , , X = inv(A)*B , X = A\B , Y = B*inv(A)
, Y = B/A. \ /
, ,
.
.
AX = I XA = I . -,
, pinv.
. .
, , A^p A .
X = A^2
X=
3
10
6 14 25
10 25 46
, A^(-p) inv(A)
p .
Y = B^(-3)
Y=
0.0053 -0.0068 0.0018
-0.0034 0.0001 0.0036
-0.0016 0.0070 -0.0051
, A^(2/3), ; .
.^ ( !) . ,
X = A.^2
A=
1
1
1
1 1
4 9
9 36
sqrtm , .. X = sqrtm(A) , X*X = A . m sqrtm , . sqrt(A), ,
A.^(1/2) ( !), .
dx/dt = Ax
x = x(t) t, A t.
.
x(t) = Atx(0)
expm(A) . 33
A=
0 -6 -1
6 2 -16
-5 20 -10
x(0)
x0 = [ 1 1 1].
101 0.01 0 t 1
X = [ ];
for t = 0 : 0.01 : 1
X = [X expm(t*A)*x0];
end
plot3(X(1,:), X(2,:), X(3,:), '-o')
(. . ). .
v,
Av = v
, V , v,
AV = V
V ,
= VV-1
.
lambda = eig(A)
- ( )
lambda =
-3.0710
-2.4645 + 17.6008i
-2.4645 - 17.6008i
, . -
.
, eig
.
[V,D] = eig(A)
V=
-0.8326 0.2003 - 0.1394i 0.2003 + 0.1394i
-0.3553 -0.2110 - 0.6447i -0.2110 + 0.6447i
-0.4248
-0.6930
-0.6930
D=
-3.0710
0
0
0
0
-2.4645+17.6008i
0
0
-2.4645-17.6008i
( V) ,
-.
, .. norm(v,2), .
V*D*inv(V), V*D/V, ,
, . , inv(V)*A*V, V\A*V, , , D.
. . ,
A=
6 12 19
-9 -20 -33
4 9 15
[V, D] = eig(A)
V=
-0.4741
0.8127
-0.3386
-0.4082
0.8165
-0.4082
-0.4082
0.8165
-0.4082
D=
-1.0000
0
0
1.0000
0
0
0
0
1.0000
.
V - ( V1
).
A u v ,
Av = u
ATu = v
U V,
,
AV = U
ATU = V
U V ,
A = U VT
mn mm U, mn
, nn V. , U V ,
, A. A , U , .
, , U mn, nn V.
, , . ,
,
. . , -, .
A - ,
. , ,
.
A=
9
6
2
4
8
7
[U,S,V] = svd(A)
U=
-0.6105 0.7174 0.3355
-0.6646 -0.2336 -0.7098
-0.4308 -0.6563 0.6194
S=
14.9359
0
0
5.1883
0
V=
-0.6925
-0.7214
0.7214
-0.6925
, U*S*V' .
.
[U,S,V] = svd(A,0)
U=
-0.6105 0.7174
-0.6646 -0.2336
-0.4308 -0.6563
S=
14.9359 0
0 5.1883
V=
-0.6925 0.7214
-0.7214 -0.6925
, U*S*V' A .
MATLAB-,
-, -, .
conv
deconv
poly
polyder
polyfit
polyval
.
.
.
.
.
polyvalm
residue
roots
.
.
( ).
.
MATLAB -, . ,
p(x) = x3 2x 5
(Wallis),
. .
MATLAB,
p = [1 0 -2 -5].
roots :
r = roots(p)
r=
2.0946
-1.0473 + 1.1359i
-1.0473 - 1.1359i
MATLAB -. poly ,
( , ).
p2 = poly(r)
p2 =
1 8.8818e-16 -2 -5
poly roots - , , .
poly :
polyval . p s = 5,
polyval(p,5)
ans =
110
. , :
p(X) = X3 - 2X 5I
X , I - . , X
X = [2 4 5; -1 0 3; 7 1 5];
p(X) .
Y = polyvalm(p, X)
Y=
377 179 439
111 81 136
490 253 639
conv
deconv. a(s) = s2 + 2s + 3 b(s) = 4s2 + 5s + 6.
a = [1 2 3]; b = [4 5 6];
c = conv(a,b)
MATLAB
c=
4 13 28 27 18
b deconv:
[q, r] = deconv(c, a)
q=
4 5 6
r=
0 0 0 0 0
r ( ). q, r , c, a deconv
c = conv(q, a) + r
polyder .
p = [1 0 -2 -5],
q = polyder(p)
q=
3
0 -2
polyder . , a b:
a = [1 3 5];
b = [2 4 6];
a*b polyder a b :
c = polyder(a, b)
c=
8 30 56 38
a/b polyder
:
[q, d] = polyder(a, b)
q=
-2 -8 -2
d=
4 16 40 48 36
q/d .
polyfit n , ( y(x)) :
p = polyfit(x, y, n)
x y , x y, . , x-y,
x = [1 2 3 4 5];
y(x)
p = polyfit(x,y,3)
p=
-0.1917 31.5821 -60.3262 35.3400
, polyfit,
( 0.1) ( plot)
.
x2 = 1 : 0.1 : 5;
y2 = polyval(p, x2);
plot(x, y, 'o', x2, y2); grid on
grid on , .
.
,
.
residue , .
. a(s) b(s),
b(s)
r
r
rn
= 1 + 2 + ... +
+ k (s)
a (s) s p1 s p 2
s pn
r - , p - , k - - .
W ( p) =
4 + 8s 1
1 + 6s 1 + 8s 2
:
b = [-4 8];
a = [1 6 8].
[r, p, k] = residue(b, a)
r=
-12
8
p=
-4
-2
k=
[ ]
residue (r, p, k) (b2, a2) , .
[b2, a2] = residue(r, p, k)
b2 =
-4
a2 =
1 6 8
.. .
, .
() ,
. , . MATLAB
, .
griddata
griddata3
griddatan
interp1
interp2
interp3
interpft
interpn
pchip
spline
.
.
(n>=3).
.
.
.
.
.
.
.
MATLAB- .
1.
interp1
. ,
() .
yi = interp1(x, y, xi, method)
y , ; x ,
( ), y; xi , y ; method
, . :
(method = 'nearest').
.
(method = 'linear').
, xi ( ).
(method = 'spline'). , .
(method = 'pchip' 'cubic'). .
.
- xi , x,
. ,
yi = interp1(x, y, xi, method, extrapval)
, extrapval.
NaN.
x .
, . ,
, . , , (
). .
,
.
. ,
, ( ).
, . , ,
.
. ,
.
- .
2. _
interpft (FFT). ,
.
.
y = interpft(x, n)
x , ,
, n - ,
.
interp2 -
.
ZI = interp2(X, Y, Z, XI, YI, method)
Z , ; X Y , ; XI YI , ( , ); method ,
. :
(method = 'nearest'). -
. .
(method = 'linear').
() . . -;
, .
(method = 'cubic'). .
.
.
. , , .
, X Y , . -
meshgrid, , , , , meshgrid. ,
. X Y ,
, , '*cubic'.
77.
1. peaks ( ).
[x, y] = meshgrid(-3 : 1 : 3);
z = peaks(x,y);
surf(x,y,z)
meshgrid(-3:1:3) x y
77; peaks(x,y) , MATLAB-
, surf(x,y,z) . .
2. ( 0.25).
[xi,yi] = meshgrid(-3:0.25:3);
3. .
zi1 = interp2(x,y,z,xi,yi,'nearest');
zi2 = interp2(x,y,z,xi,yi,'bilinear');
zi3 = interp2(x,y,z,xi,yi,'bicubic');
.
surf(xi,yi,zi1)
surf(xi,yi,zi2)
surf(xi,yi,zi3)
nearest
bilinear
bicubic
,
contour.
contour(xi,yi,zi1)
nearest
contour(xi,yi,zi2)
bilinear
contour(xi,yi,zi3)
bicubic
, . , ,
. , , , ,
, .
MATLAB
. , MATLAB ,
(
) :
Optimization .
Signal Processing , .
Spline .
Statistics ,
.
Wavelet - .
! MATLAB ,
!
, n- 1 n n1.
,
. MATLAB ,
- , . , , , 24 3
- 243.
, MATLAB- datafun
8 .
, , 24 . , count
count =
11
11
7
13
14
17
11 13
43
51
38 46
61 132
75 135
38 88
28 36
12 12
18
27
18 19
17 15
19 36
32 47
42 65
57 66
44 55
114 145
35 58
11 12
13 9
10
9
9
11
20
9
69
76
186
180
115
55
14
30
29
18
48
10
92
151
90
257
68
15
15
7
145
257
mu =
32.00
46.5417
65.5833
sigma =
25.3703
41.4057 68.0281
count.
. ,
[mx,indx] = min(count)
mx =
7
indx =
2 23 24
, 2
( ) 23 24 . .
, , mu ( )
e = ones(24, 1)
x = count - e*mu
. ,
MATLAB ,
,
. ,
min(count(:))
ans =
7
count(:) 243 721.
MATLAB- :
cov , () .
,
.
corrcoef .
cov count
cov(count(:,1))
ans =
643.6522
, cov . .
.
mn, n-by-n ,
.
corrcoef ,
, .
. (-) ; . mn, nn.
. ,
corrcoef(count)
ans =
1.0000 0.9331 0.9599
0.9331 1.0000 0.9553
0.9599 0.9553 1.0000
,
, .
MATLAB .
diff
. .
.
.
gradient
del2
diff ,
diff(X) [X(2) -X(1) X(3) -X(2) ... X(n) -X(n-1)]. , A,
A = [9 -2 3 0 1 5 4];
diff(A)
MATLAB
ans =
-11 5 -3 1 4 -1
, diff . , diff ,
( ),
.. diff x.
()
diff(x) == 0
all(diff(x) > 0)
all(diff(diff(x)) == 0)
:
()
NaN, MATLAB- . NaN 0/0. . ,
NaN . MATLAB NaN .
. , NaN, NaN.
, , 33, NaN.
a = magic(3);
a(2,2) = NaN;
a=
8 1 6
3 NaN 7
4 9 2
:
sum(a)
ans =
15 NaN 15
NaN NaN . NaN-
. .
i = find( ~ isnan(x));
x = x(i)
x = x (find( ~ isnan(x)))
x = x ( ~ isnan(x));
x (isnan(x)) = [ ];
,
NaN,
NaN-
NaN- ().
NaN-
X (any(isnan(X)), :) = [ ];
X NaN
,
NaN-. ,
count, ()
count
mu = mean(count)
sigma = std(count)
mu =
32.0000 46.5417 65.5833
sigma =
25.3703 41.4057 68.0281
, :
[n, p] = size(count)
outliers = abs(count - mu(ones(n, 1),:)) > 3*sigma(ones(n, 1),:);
nout = sum(outliers)
nout =
1
count(any(outliers'),:) = [ ];
,
( ) . , , . \ (backslash). , y
t.
t = [0 0.3 0.8 1.1 1.6 2.3]';
y = [0.5 0.82 1.14 1.25 1.35 1.40]';
plot(t,y,'o'); grid on
()
:
- (linear-in-the-parameters)
, ,
:
y = a0 + a1t + a2t2
a0 , a1 a2 (), . ,
63:
X = [ones(size(t)) t
X=
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
0
0.3000
0.8000
1.1000
1.6000
2.3000
t.^2]
0
0.0900
0.6400
1.2100
2.5600
5.2900
\ :
a = X\y
a=
0.5318
0.9191
- 0.2387
,
y = 0.5318 + 0.9191t 0.2387 t2
( 0.1)
.
T = (0 : 0.1 : 2.5)';
Y = [ones(size(T)) T T.^2]*a;
plot(T,Y,'-',t,y,'o'); grid on
, . , -
.
- 1
, - . ,
y = a0 + a1-t + a2t-t
, a0 , a1 a2 . , X, \ :
X = [ones(size(t))
exp(- t)
t.*exp(- t)];
a = X\y
a=
1.3974
- 0.8988
0.4097
,
1 - .
,
.
, . , y x1 x2
x1 = [0.2 0.5 0.6 0.8 1.0 1.1]' ;
x2 = [0.1 0.3 0.4 0.9 1.1 1.4]' ;
y = [0.17 0.26 0.28 0.23 0.27 0.24]' ;
y = a0 + a1x1 + a2x2
a0 , a1 a2 . , X , \ .
X = [ones(size(x1)) x1 x2];
a = X\y
a=
0.1018
0.4844
-0.2847
,
y = 0.1018 + 0.4844x1 0.2847x2
.
Y = X*a;
MaxErr = max(abs(Y - y))
MaxErr =
0.0038
, .
MATLAB
() ( Basic Fitting interface). ,
,
. :
, , 10 .
.
( ).
.
.
.
MATLAB-.
, ,
, , . ,
.
.
, :
1. .
2. Basic Fitting Tools .
3. More . c
(. ), Less.
.
Select data ( ) , , ( ). () . ,
. .
,
.
(Plot Editor), .
Center and scale X data ( X)
, ( ) ( ).
. , , .
Plot fits ( )
.
Check to display fits on figure ( )
, .
.
Numerical results ( )
,
.
Fit ( ) . . , Plot fits. , , Plot fits.
Coefficients and norm of residuals ( )
, Fit. , Numerical Results ,
, Plot fits.
Save to workspace ( ) ,
.
Find Y = f(X) .
Enter value(s) ( ) , MATLAB . Evaluate (), . Fit.
Save to workspace ( ) ,
.
Plot results ( ) , .
MATLAB . .
,
. , , -.
y = filter(b, a, x)
x , a b, y. filter . filter
, ,
, n ()
, na , a , nb , b. y(n) , x(n) x(n-1) ..., y(n-1) y(n-2) ...
a(1) y(n) = b(1) x(n) + b(2) x(n-1) + + b(nb) x(n-nb+1)
- a(2) y(n-1) - - a(na) y(n-na+1)
, , , 4 . :
y(n) = (1/4) x(n) + (1/4) x(n-1) + (1/4) x(n-2) + (1/4) x(n-3)
:
a = 1;
count . , count x :
x = count(:, 1);
4
y = filter(b, a, x). ,
.
t = 1:length(x) ;
plot(t, x, '-.', t, y, '-');
grid on
legend('Original Data','Smoothed Data',2)
- ,
4 .
, Signal Processing Toolbox
.
MATLAB
. , (row) (column).
,
() , .
. ,
:
1 , .
2. .
3.
(page) 3 .
, , 2
(2,3,2) (. ).
, .
, , 4 . -,
.
,
datatypes.
,
.
. ,
.
A = [5 7 8; 0 1 9; 4 3 6];
33, .
A(:,:,2) = [1 0 4; 3 5 6; 9 8 7].
MATLAB
A(:, : ,1) =
5 7 8
0 1 9
4 3 6
A(:, :, 2) =
1 0 4
3 5 6
9 8 7
, .
.
:
.
.
, ,
..
MATLAB,
, :
A(:, :, 3) = 5;
A(:, :, 3)
ans =
5 5 5
5 5 5
5 5 5
3332
A(:, :, 1, 2) = [1 2 3; 4 5 6; 7 8 9];
A(:, :, 2, 2) = [9 8 7; 6 5 4; 3 2 1];
A(:, :, 3, 2) = [1 0 1; 1 1 0; 0 1 1];
, MATLAB A
, (
, ,
33).
MATLAB-.
MATLAB-
randn, ones, zeros,
.
. ,
432 :
B = randn(4,3,2).
, repmat. (
11) .
B = repmat(5,[3 4 2])
B(:, :, 1) =
5 5 5 5
5 5 5 5
5 5 5 5
B(:, :, 2) =
5 5 5 5
5 5 5 5
5 5 5 5
! 0,
(empty array) . , 10020
.
cat.
cat ;
.
B = cat (dim,A1,A2...)
1, 2 .. . dim ,
. ,
cat
B = cat (3, [2 8; 0 5], [1 3; 7 9])
B(:, :, 1) =
2
0
8
5
B(:, :, 2) =
1
7
3
9
cat . ,
. , ,
:
A = cat (3, [9 2; 6 5], [7 1; 8 4])
B = cat (3, [3 5; 0 1], [5 6; 2 1])
D = cat (4, A, B, cat (3, [1 2; 3 4], [4 3; 2 1])).
cat , ,
. , 2212
C = cat (4, [1 2; 4 5], [7 8; 3 2])
cat
, .
dim 5, 22112.
. 8
.
size ( ), ndims ( )
whos (
MATLAB).
size(C)
ans =
2
ndims(C)
ans =
4
, , . () -
.
: , -
, .
nddata 1053:
nddata = fix (8*randn (10, 5, 3));
(3,2) 2 nddata nddata(3,2,2).
.
, , . , (2,1), (2,3), (2,4)
3 nddata,
nddata (2, [1 3 4], 3).
.
MATLAB- (colon)
. , 2
nddata nddata(:, 3, 2).
. , nddata(2:3,2:3,1) () 22, 1 nddata.
.
. , 44 :
C = zeros (4,4)
, 22 nddata
C(2:3,2:3) = nddata (2:3,1:2,2)
,
A(:, :, 2) = 1:10
, , .
, .
MATLAB . ,
, ,
. ,
A(1,:,2) = 1:10.
(Reshaping)
, MATLAB , .
. ,
, . reshape
.
reshape .
.
,
nddata ( ).
B = reshape(nddata,[6 25])
C = reshape(nddata,[5 3 10])
D = reshape(nddata,[5 3 2 5])
.
MATLAB ,
, .
B = repmat (5, [2 3 1 4] ) ;
size(B)
ans =
squeeze .
C = squeeze(B);
size(C)
ans =
2
squeeze - .
MATLAB-
.
, , , .
, sum, mean, .., .
. ,
. , cross,
, , 3.
! , , ,
.
MATLAB-, ,
,
. , sin , . . , ,
( ).
, ,
.
, , matfun ,
. ,
matfun, , ^, \, /, .
.
, ,
. ,
A = cat (3 , [1 2 3; 9 8 7; 4 6 5], [0 3 2; 8 8 4; 5 3 5], [6 4 7; 6 8 5; 5 4 3]);
eig :
eig(A)
??? Error using eig
Input arguments must be 2-D.
, , eig .
, (,
):
eig(A(:, :, 2))
ans =
2.6260
12.9129
2.7131
! , , squeeze. , eig (A(2,:,:))
[1 3 3]. eig(squeeze(A(2, :, :))), ,
eig .
:
( ) . .
. , ,
,
() .
-
RGB. , RGB
,
(Red), (Green) (Blue) - . -
.
, , ,
.
RGB. , ,
red_plane = RGB (:,:,1);
subimage = RGB (20:40, 50:85, :)
RGB , , . , , . ,
, , .
, .
(. ).
, ,
,
mean (mean (mean (TEMP)))
TEMP .
( (2,2))
, ,
B = TEMP (2, 2, :).
MATLAB
(structure) (cell). ( )
, . ( )
MATLAB, ,
, MATLAB-. ,
.
. ,
.
MATLAB-,
fieldnames
getfield
isfield
isstruct
rmfield
setfield
struct
struct2cell
,
,
cell
cell2struct
celldisp
cellfun
cellplot
deal
iscell
num2cell
,
. . ,
, (name),
, (billing),
(test) .
, .
11, 5
11.
, .
:
.
struct.
.
11
. MATLAB
. , 11 ,
. :
struct
struct
repmat
struct
weather(3) =
weather(3)
struct('temp',72,'rainfall',0.0); .
, weather(1)
weather(2), .
weather =
repmat (struct ('temp', 72,
weather
'rainfall', 0.0), 1, 3);
.
weather =
weather
struct ('temp',{68, 80, 72},
'rainfall', {0.2,0.4,0.0} );
,
.
.
,
. , . , , , .
. , 12
mypatients = patient(1:2)
1x2 struct array with fields:
name
billing
test
mypatients patient:
mypatients(1)
ans =
name: 'John Doe'
billing: 127
test: [3x3 double].
, (.) , :
str = patient(2) . name
str =
Ann Lane
,
. ,
. ,
. ,
test2b = patient(3).test(2,2)
test2b =
153
, ,
patient(3).test(2,2) = 7
. , 13,
billing fields.
bills = [patient.billing]
bills =
127.0000 28.5000 504.7000
, , test .
tests = {patient(1:2).test}
tests =
[3x3 double] [3x3 double]
setfield getfield
. , , ,
, fieldnames
M- setfield getfield.
getfield
f = getfield(array,{array_index},'field',{field_index})
field_index , array_index
11.
f = array(array_index).field(field_index)
, name patient
str = getfield(patient,{2},'name')
, setfield
f = setfield (array,{array_index},'field',{field_index},value)
. size. size ,
.
(n)., size . ,
patient 13, size(patient) [1 3].
size(patient(1,2).name) (1,2) patient.
. , patient
patient(2).ssn = '000000000'
patient(2).ssn . ssn,
, .
rmfield.
struc2 = rmfield(array,'field')
array , 'field' , .
, name patient, :
patient = rmfield(patient,'name')
,
MATLAB. ,
. , -
test patient(2):
mean((patient(2).test)')
. billing patient :
total = 0;
for j = 1:length(patient)
total = total + patient(j).billing;
end
, MATLAB .
(, array.field) . , ,
total = sum ([patient.billing])
,
(comma-separated list)
total = sum ([patient(1).billing , patient(2).billing ,...])
, .
- . .
, , .
, ,
. 15
, . 15 ,
, .
concen, . lead (), mercury (),
chromium ().
function [r1, r2] = concen(toxtest);
% Create two vectors. r1 contains the ratio of mercury to lead
% at each observation. r2 contains the ratio of lead to chromium.
r1 = [toxtest.mercury]./[toxtest.lead];
r2 = [toxtest.lead]./[toxtest.chromium];
% Plot the concentrations of lead, mercury, and chromium
% on the same plot, using different colors for each.
lead = [toxtest.lead];
mercury = [toxtest.mercury];
chromium = [toxtest.chromium];
plot(lead,'r'); hold on
plot(mercury,'b')
plot(chromium,'y'); hold off
. r1 , r2 . , , ( , , ).
test
test(1).lead = .007; test(2).lead = .031; test(3).lead = .019;
test(1).mercury = .0021; test(2).mercury = .0009;
test(3).mercury = .0013;
test(1).chromium = .025; test(2).chromium = .017;
test(3).chromium = .10;
, . , , -
,
. , RGB 128128,
: RED, GREEN BLUE.
,
, .
A.r = RED;
A.g = GREEN;
A.b = BLUE;
, . ,
red_plane = A.r;
,
. , A(2), A(3), , .
, . :
red_sub = A.r (2:12, 13:30);
grn_sub = A.g (2:12, 13:30);
blue_sub = A.b (2:12, 13:30);
,
.
for i = 1:size(RED,1)
for j = 1:size(RED,2)
B(i,j) .r = RED(i,j);
B(i,j) .g = GREEN(i,j);
B(i,j) .b = BLUE(i,j);
end
end
, :
Bsub = B(1:10, 1:10);
,
:
red_plane = zeros(128,128);
for i = 1 : (128128)
red_plane(i) = B(i).r;
end
, . , ,
.
-
.
,
:
. ,
amount :
)
avg = mean(A.amount);
)
avg = mean([B.amount]);
,
. -, client.m,
. ,
:
function client(name,address, amount)
disp(name)
disp(address)
disp(amount)
client ,
client(A.name(2,:),A.address(2,:), A. amount (2,:))
function client(B)
disp(B)
client ,
client(B(2))
.
,
name address ,
.
. ,
, .
, .
, struct,
struct
struct . ,
11 nest :
A = struct('data',[3 4 7; 8 0 1],'nest',struct('testnum','Test 1', 'xdata',[4 2 8],'ydata',[7 1 6]));
:
A(2).data = [9 3 2; 7 6 5];
A(2).nest.testnum = 'Test 2';
A(2).nest.xdata = [3 4 2];
A(2).nest.ydata = [5 0 9];
,
. , , . , , , :
(1) A(1).nest.
xdata A(2)
A(2).nest.xdata.
2 ydata A(1), A(1).nest.ydata(2).
MATLAB . ,
, , .
, .
:
.
cell, .
,
. MATLAB . :
. {}. , 22.
A(1,1) = {[1 4 3; 0 5 8; 7 2 9]};
A(1,2) = {'Anne Smith'};
A(2,1) = {3+7i};
A(2,2) = {pi:pi/10:pi};
! {} , [ ]
. .
, . .
A{1,1} = [1 4 3; 0 5 8; 7 2 9];
A{1,2} = 'Anne Smith';
A{2,1} = 3+7i;
A{2,2} = pi:pi/10:pi;
, , . .
! ,
,
. , MATLAB , .
MATLAB . .
A=
[3x3 double]
'Anne Smith'
[3.0000+ 7.0000i] [1x21 double]
, celldisp. cellplot. , ,
MATLAB , .
. , 22 33.
A(3,3) = {5};
.
, {}, , .
, , (. ).
, . ,
C = {[1 2], [3 4]; [5 6], [7 8]};
22
,
, .
cell
cell . , 22.
B = cell(2, 3);
:
B(1,3) = {1:3};
:
.
, .
- .
.
. , .
N 22:
N{1,1} = [1 2; 4 5];
N{1,2} = 'Name';
N{2,1} = 2 4i;
N{2,2} = 7;
N{1,2}
c = N{1,2}
MATLAB
c=
Name
!
, . ,
A{1, :} = value B = A{1, :} .
. , (2,2) N{1,1}, :
d = N{1,1} (2,2)
d=
5
,
. .
. ,
,
,
.
A(:, 2) = [ ]
, .
()
, reshape. ,
.
A = cell(3, 4);
size(A)
ans =
3 4
B = reshape(A,6,2);
size(B)
ans =
6 2
MATLAB- :
.
.
.
( ).
, MATLAB .
, T, . T{1:5} T,
[ T{1} , T{2} , T{3} , T{4} , T{5} ]
C:
C(1) = {[1 2 3]};
C(2) = {[1 0 1]};
C(3) = {1:10};
C(4) = {[9 8 7]};
C(5) = {3};
C(1) C(2) conv,
d = conv(C{1:2})
d=
1
C{2:4}
ans =
1
ans =
1 2
0 1
ans =
9
10
,
B = [ C{1}; C{2}; C{4} ]
B=
1 2 3
1 0 1
9 8 7
,
, :
for i = 1:length(A)
M{i} = sum(A{1,i});
end
, . , :
.
.
.
.
,
:
33, , .
15 , .
345, 5 .
, .
, ,
.
TEST.
[newdata, name] = deal (TEST{1:2})
TEST:
newdata = TEST.measure
name = TEST.name
(,
(leaf cells).) , cell, .
. ,
clear A
A(1,1) = {magic(5)};
A(1,2) = { { [ 5 2 8; 7 3 0; 6 7 3] 'Test 1'; [2 4i 5 + 7i] {17 [ ] } } }
A=
[5x5 double] {2x2 cell}
,
. cell (1,2) A.
22 .
cell
cell, cell
. ,
1. 12
A = cell (1, 2);
2. 22 A(1,2).
A(1,2) = {cell(2,2)};
3. ,
A(1,1) = {magic(5)};
A{1,2}(1,1) = {[5 2 8; 7 3 0; 6 7 3]};
A{1,2}(1,2) = {'Test 1'};
A{1,2}(2,1) = {[24i 5+7i]};
A{1,2}(2,2) = {cell(1,2)}
A{1,2}{2,2}(1) = {17};
.
, 3 .
.
,
. ,
55 (1,1) A{1,1}.
33 (1,1) (1,2) A{1,2}{1,1}.
22 (1,2) A{1,2}.
(2,2) (1,2) A{1,2}{2,2}{1,2}.
, . , F:
F{1,1} = [1 2; 3 4];
F{1,2} = [1 0; 0 1];
F{2,1} = [7 8; 4 1];
F{2,2} = [4i 3+2i; 1 8i 5];
F NUM.
for k = 1:4
for i = 1:2
for j = 1:2
NUM(i,j,k) = F{k}(i,j);
end
end
end
, for :
G = cell(1,16);
for m = 1:16
G{m} = NUM(m);
end
,
c_str = cell(1,2);
c_str{1}.label = '12/2/94 12/5/94';
c_str{1}.obs = [47 52 55 48; 17 22 35 11];
c_str{2}.xdata = [0.03 0.41 1.98 2.12 17.11];
c_str{2}.ydata = [3 5 18 0 9];
c_str{2}.zdata = [0.6 0.8 1 2.2 3.4];
l c_str , , - . 2 .
, , . , , . :
cell_array{index}.field
, label 1
c_str{1}.label
,
. cat, .
, :
A{1,1} = [1 2; 4 5];
A{1,2} = 'Name';
A{2,1} = 2 4i;
A{2,2} = 7;
B{1,1} = 'Name2';
B{1,2} = 3;
B{2,1} = 0:1:3;
B{2,2} = [4 5]';
C = cat(3,A,B);
,
. ,
, cat.
patient(1,1,1).name = 'John Doe';
patient(1,1,1).billing = 127.00;
patient(1,1,1).test = [79 75 73; 180 178 177.5; 220 210 205];
patient(1,2,1).name = 'Ann Lane';
patient(1,2,1).billing = 28.50;
patient(1,2,1).test = [68 70 68; 118 118 119; 172 170 169];
patient(1,1,2).name = 'Al Smith';
patient(1,1,2).billing = 504.70;
patient(1,1,2).test = [80 80 80; 153 153 154; 181 190 182];
patient(1,2,2).name = 'Dora Jones';
patient(1,2,2).billing = 1173.90;
patient(1,2,2).test = [73 73 75; 103 103 102; 201 198 200];
. . , test patient(1,1,2):
sum((patient(1,1,2).test));
, billing patient:
total = sum([patient.billing]);
MATLAB-
MATLAB :
- MATLAB (scripts),
(), (functions), . -
MATLAB.
:
1. - .
2. - -.
-
, -,
MATLAB
( ,
)
- ?
, -. , fact, :
function f = fact (n)
%
% FACT Factorial.
% (H1 line)
% FACT(N) returns the factorial of N, H! % (Help text)
% usually denoted by N!
% Put simply, FACT(N) is PROD(1:N).
f = prod(1:n); %
, MATLAB:
. ,
.
type fact
, , , 5-
fact(5)
ans =
120
! - (), MATLAB-.
MATLAB-,
MATLAB-.
-,
. ,
, .
;
. ,
, MATLAB-, .
rho theta,
% An Mfile script to produce
% "flower petal" plots
theta = pi:0.01:pi;
rho(1,:) = 2*sin(5*theta).^2;
rho(2,:) = cos(10*theta).^3;
rho(3,:) = sin(theta).^2;
rho(4,:) = 5*cos(3.5*theta).^3;
for i = 1:4
polar(theta,rho(i,:))
pause
end
%
%
-, petals.m.
MATLAB-. petals () MATLAB
. pause
- (, Return). ,
, Return . ; petals
MATLAB-. , (i, theta, rho) . , whos .
-,
. , ,
MATLAB-.
average -, .
function y = average(x)
% AVERAGE Mean of vector elements.
% AVERAGE(X), where X is a vector, is the mean of vector elements.
% Non-vector input results in an error.
[m,n] = size(x);
if (~((m == 1) | (n == 1)) | (m == 1 & n == 1))
error('Input must be a vector')
end
y = sum(x) / length(x) ;
%
, ( , ). -, average.m. average
. ,
z = 1:99;
average(z)
ans =
50
-
:
(The Function Definition Line)
H1 (The H1 Line)
(Help Text)
(The Function Body)
(Comments)
MATLAB , - , . average
:
input argument ;
function name ;
output argument ;
keyword ;
MATLAB- ,
. ,
. , ,
.
function [x,y,z] = sphere(theta, phi, rho)
, , ,
function printresults(x)
function [ ] = printresults(x)
, ,
.
H1
H1, (Help text),
, . , H1 (%). average
% AVERAGE Mean of vector elements.
( )
,
help function_name
(help _)
, lookfor H1.
-,
.
() - , ,
H1. average
% AVERAGE(X), where X is a vector, is the mean of vector elements.
% Nonvector input results in an error.
(% (), ,
% . ).
help function_name, MATLAB , ( ). MATLAB , . , help sin
SIN
Sine.
SIN(X) is the sine of the elements of X.
(SIN(X) )
MATLAB,
. , , ,
, , . , average :
[m,n] = size(x);
if (~((m == 1) | (n == 1)) | (m == 1 & n == 1)) % Flow control
error('Input must be a vector') % Error message display
end
y = sum(x)/length(x); % Computation and assignment
, (%).
-, . ,
% Add up all the vector elements.
y = sum(x) % Use the sum function.
( % .
y = sum(x)
% sum)
,
H1 . H1 ,
1,
. ,
-. . ,
.
MATLAB- , . MATLAB
32 . ; , . .
, MATLAB-, .m . , average.m.
, .
, , ,
,
.
- MATLAB-, . , , , .
MATLAB , :
1. , .
2. , (subfunction),
MATLAB, -, .
3. , (private function), ,
Private (private directory) , - .
4. , MATLAB-. MATLAB
. , MATLAB e .
- -,
MATLAB , .
. clear,
MATLAB-.
clear
MATLAB-.
clear function_name
clear functions
clear all
P-
, (P-code) , .
,
pcode average
average.m
average.p. MATLAB- . , MATLAB ,
. , (GUI) . -
GUI . , , ,
.
MATLAB
, MATLAB . , , MATLAB , . , , MATLAB ( ) .
- , MATLAB-, . , .
MATLAB-, , , - . , , , , , . , , , .
nargin nargout . . ,
function c = testarg1(a,b)
if (nargin == 1)
c = a.^2;
elseif (nargin == 2)
c = a + b;
end
, .
, .
varargin varargout
. varargin MATLAB . varargout, , MATLAB
. ,
, ,
.
function testvar (varargin)
for i = 1:length (varargin)
x(i) = varargin{i}(1);
y(i) = varargin{i}(2);
end
xmin = min(0,min(x));
ymin = min(0,min(y));
axis([xmin fix(max(x)) + 3 ymin fix(max(y)) + 3])
plot(x,y)
testvar ; ,
testvar ([2 3], [1 5], [4 8], [6 5], [4 2], [2 3])
testvar ([1 0], [3 5], [4 2], [1 1])
varargin
varargin , . ,
y(i) = varargin{i} (2);
,
. {i} i-
varargin, (2) .
varargout
, varargout. ,
nargout. ,
, x, y . [xi yi], testvar.
function [varargout] = testvar2 (arrayin)
for i = 1:nargout
varargout {i} = arrayin (i, :)
end
for .
testvar2:
a = {1 2; 3 4 ; 5 6 ; 7 8; 9 0};
[p1, p2, p3, p4, p5] = testvar2 (a);
varargin varargout
varargin varargout ,
.
,
. ,
varargin varargout.
function [out1,out2] = example1(a,b,varargin)
function [i,j,varargout] = example2(x1,y1,x2,y2,flag)
MATLAB-, -,
,
. , , , ,
,
. , - ,
, .
, ,
- - (Lotka-Volterra),
-.
dy1/dt = y1 - y1y2
dy2/dt = y2 - y1y2
- lotka.m.
function yp = lotka(t,y)
global ALPHA BETA
yp = [y(1) ALPHA*y(1)*y(2); y(2) + BETA*y(1)*y(2)];
global ALPHA BETA
ALPHA = 0.01
BETA = 0.02
[t,y] = ode23('lotka',0,10,[1; 1]);
plot(t,y)
mlock
munlock
mislocked
-
-
, -
, -.
ans
eps
realmax
realmin
pi
i, j
inf
().
, MATLAB
ans.
.
, MATLAB .
.
.
3.1415926535897...
.
. n/0 n
NaN
computer
version
, inf.
(Not-a-Number). 0/0
inf/inf NaN,
NaN. n/0, n , NaN.
.
, MATLAB-.
, .
x = 2*pi;
A = [3 + 2i 7 8i];
tol = 3*eps;
MATLAB 14 ( ) .
. 00 .
. 14 . , , , user class ( ),
.
MATLAB- .
.
, .
, double.
MATLAB :
, .
, .
, AND ( ), OR ( ),
NOT ( ).
MATLAB
+
.*
./
.\
+
:
.^
.
*
/
\
^
( )
, MATLAB-
.
, ,
. , - , MATLAB
;
(scalar expansion).
A = magic(3)
A=
8
3
1
5
6
7
3*A
ans =
24
9
12
3 18
15 21
27 6
MATLAB
<
<=
>
>=
==
~=
MATLAB-
. . -
, , A B.
A = [2 7 6; 9 0 5; 3 0.5 6];
B = [8 7 0; 3 2 5; 4 1 7];
A == B
ans =
0 1 0
0 0 1
0 0 0
,
. ,
, MATLAB .
, , 1. ,
, 0.
, - ( ),
. ,
A == [ ]
, 00 11. , isempty(A).
MATLAB
&
AND ( )
OR
( )
NOT ( )
! , ops , () .
, :
(&), , .
, , .
u = [1 0 2 3 0 5];
v = [5 6 1 0 0 7];
u&v
ans =
0 0 1 0 0 1
, ( | ),
. . , , . u v
u|v
ans =
1
1 1 1 0 1
, ~ .
, , . , ( ), ()
. u
~u
ans =
0 1 0 0 1 0
MATLAB- . ,
, . , , .
, MATLAB .
xor
. ,
1
, -
1,
;
. all
a = 1;
b = 1;
xor(a,b)
ans =
0
A = [0 1 2; 3 5 0]
A=
0 1 2
3 5 0
all(A)
ans =
010
v = [5 0 8];
any(v)
ans =
1
all
any
,
; 0. all , any
.
MATLAB- . , isnan
1 NaN; isinf 1 Inf.
ops.
find
find , . () . , find .
.
, find 8 100:
A = magic(4)
A=
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
i = find (A > 8);
A(i) = 100
A=
100 2
5 100
100 7
4 100
3 100
100 8
6 100
100 1
find , ( ).
, , .
, MATLAB .
, () . MATLAB- , ,
.
1. ().
2. (.'), (.^), - ('),
(^).
3. (+), (-), (~).
4. (.*), (./), (.\), (*),
(/), (\).
5. (+), (-).
6. (:).
7. (<), (<=), (>),
(>=), (==), (~=).
8. (&).
9. (|).
, .
A = [3 9 5];
B = [2 1 5];
C = A ./ B.^2
C=
0.7500 9.0000 0.2000
C = (A. / B) .^2
C=
2.2500 81.0000 1.0000
,
b = sqrt (A(2)) + 2*B(1)
b=
7
(Flow Control)
MATLAB- 8 :
if, else elseif, ,
.
switch, case otherwise, , .
while ,
.
for () .
continue for while ,
.
break , for
while.
try...catch ,
.
return .
, , end
.
! MATLAB-,
for while (. ).
if, else, and elseif
if , .
if ( )logical_expression
end
, 1, MATLAB
, if end . -
if X
end
if all(X(:))
end
if , else elseif :
else . , , if (, , elseif) .
elseif , , if (, , elseif), .
elseif , , . elseif
if.
if n < 0
% n ,
disp('Input must be positive');
elseif rem (n, 2) == 0 % n , 2.
A = n/2;
else
A = (n+1) /2; % n , 1
% .
end
if
, if, ,
. , ,
if A
S1
else
S0
end
S0.
switch
switch , .
switch ( )
case 1
% == 1
case 2
% == 2
.
.
.
otherwise
%
% , case
end
:
switch ..
case. case,
switch.
,
switch. , switch. , case , MATLAB case
otherwise. , case.
, otherwise; , - case. otherwise end.
end.
switch ,
, case. - () , == . , , strcmp(,) ( strcmp
) .
switch.
input_num . input_num
1, 0, 1, case .
input_num ,
otherwise 'other value' ( ).
switch input_num
case 1
disp('negative one');
case 0
disp('zero');
case 1
disp('positive one');
otherwise
disp('other value');
end
! C, switch
MATLAB- case,
; case .
switch case,
,
.
switch var
case 1
disp('1')
case {2,3,4}
disp('2 or 3 or 4')
case 5
disp('5')
otherwise
disp('something else')
end
while
while ,
, , .
,
.
while expression
statements
end
expression , , .
, all
any. , n, n!
100-
n = 1;
while prod(1:n) < 1e100
n = n + 1;
end
while break.
, ,
.
while
while , ,
while A
S1;
end
S1 A .
for
for
.
for index = start:increment:end
statements
end
index start end increment. , 1, , . ,
end;
.
, .
for i = 2:6
x(i) = 2*x (i1);
end
for:
for i = 1:m
for j = 1:n
A (i, j) = 1/(i + j 1) ;
end
end
! MATLAB-
for while (. ).
for . , m n .
for i = A
statements
end
I A(: , k). k 1;
k 2 , , k n. ,
n , n A. ,
i , A.
continue
continue for while,
. , continue for while , .
.
break
break , for while. ,
. , break
.
try ... catch
try ... catch
try
statement,
...,
statement,
catch,
statement,
...,
statement,
end
try catch
, - . () catch end.
, , lasterr ( ). catch end, MATLAB , catch end try ... catch.
return
return
( ,
) . return keyboard ( ,
, ; ). -
, . return
.
1. MATLAB
1. matlab\general
-
2. matlab\ops
-
3. matlab\lang
-
4. matlab\elmat
-
5. matlab\elfun
-
6. matlab\specfun
-
7. matlab\matfun
-
8. matlab\datafun
-
9. matlab\audio
-
10. matlab\polyfun
-
11. matlab\funfun
-
(ODE solvers)
12. matlab\sparfun
-
13. matlab\graph2d
-
14. matlab\graph3d
-
15. matlab\specgraph -
16. matlab\graphics
- (Handle Graphics)
17. matlab\uitools
-
(Graphical User Interface (GUI))
18. matlab\strfun
-
19. matlab\iofun
- /
20. matlab\timefun
-
21. matlab\datatypes -
22. matlab\verctrl
-
23. matlab\winfun
- Windows (DDE/ActiveX)
24. matlab\demos
- MATLAB
25. toolbox\local
- (Preferences)
26. simulink\simulink - Simulink
27. simulink\blocks
- () Simulink
28. simulink\simdemos - Simulink 4
29. simdemos\aerospace - Simulink:
30. simdemos\automotive - Simulink:
76. lmi\lmictrl
77. lmi\lmilab
- , .
2. helpwin
3.
4.
5.
6.
7.
8.
- , (help
browser).
helpdesk - .
support - Web- MathWorks.
demo
- .
java
- Java MATLAB-.
ver
- MATLAB-, SIMULINK- .
whatsnew - .
- .
-
, , .
11. workspace - (Workspace
Browser), .. (GUI)
.
12. clear
- .
13. pack
- .
14. load
- .
15. save
- .
16. quit
- MATLAB.
- MATLAB-
.
18. type
- .- .
19. edit
- /.
20. open
- .
21. which
- ( )
.
22. pcode
- - .
23. inmem
- .
24. mex
- -.
- - .
- .
- () MATLAB .
- .
- (beep).
- (Tools to
locate dependent functions of an M-file).
65. depfun
66. depdir
67. inmem
- , -.
- , ,
-.
- .
3.
- matlab\ general
(Arithmetic operators)
1. plus
- Plus
+
2. uplus
- Unary plus
+
3. minus
- Minus
4. uminus - Unary minus
5. mtimes - Matrix multiply
*
6. times
- Array multiply
.*
7. mpower - Matrix power
^
8. power - Array power
.^
9. mldivide - Left matrix divide
\
10. mrdivide - Right matrix divide
/
11. ldivide - Left array divide
.\
12. rdivide - Right array divide
./
13. kron
- Kronecker tensor product
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
(Relational operators)
14. eq
15. ne
16. lt
17. gt
18. le
19. ge
- Equal
- Not equal
- Less than
- Greater than
- Less than or equal
- Greater than or equal
==
~=
<
>
<=
>=
(Logical operators)
20. and
21. or
22. not
23. xor
24. any
25. all
- Logical AND
&
- .
- Logical OR
|
- .
- Logical NOT
~
- .
- Logical EXCLUSIVE OR
- .
- , .
- , .
(Special characters.)
26. colon
27. paren
28. paren
29. paren
30. punct
31. punct
32. punct
33. punct
34. punct
35. punct
36. punct
37. punct
38. punct
39. punct
40. punct
41. transpose 42. ctranspose 43. horzcat 44. vertcat 45. subsasgn 46. subsref 47. subsindex -
:
()
[]
{}
@
.
.
..
...
,
;
%
!
=
'
.'
'
[,]
[;]
( ),{ },.
( ),{ },.
- .
- .
- .
.
- .
.
.
.
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
.
.
-
(Bitwise operators)
48. bitand
49. bitcmp
50. bitor
51. bitmax
52. bitxor
53. bitset
54. bitget
55. bitshift
- (Bit-wise AND).
- n- .
- (Bit-wise OR).
- .
- (Bit-wise XOR).
- .
- .
- 2.
(Set operators)
56. union
- .
57. unique - .
58. intersect - .
59. setdiff - .
60. setxor - .
61. ismember - .
4.
(Elementary math functions).
matlab\elfun
(Trigonometric)
1. sin
2. sinh
3. asin
4. asinh
5. cos
6. cosh
7. acos
8. acosh
9. tan
10. tanh
11. atan
12. atan2
13. atanh
14. sec
15. sech
16. asec
17. asech
18. csc
19. csch
20. acsc
21. acsch
22. cot
23. coth
24. acot
25. acoth
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- 4- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
(Exponential)
26. exp
27. log
28. log10
29. log2
- .
- .
- 10.
- 2 .
30. pow2
- 2.
31. sqrt
- .
32. nextpow2 - 2.
(Complex)
33. abs
- .
34. angle
- .
35. complex -
.
36. conj
- .
37. imag
- .
38. real
- .
39. unwrap
- .
40. isreal - , .
41. cplxpair - - .
(Rounding and remainder)
42. fix
- .
43. floor
- .
44. ceil
-
45. round
- .
46. mod
- (Modulus or signed remainder after
division).
47. rem
- .
48. sign
- (Signum).
5.
(Elementary matrices and matrix manipulation)
matlab\elmat
(Elementary matrices)
1. zeros
- ( ).
2. ones
- .
3. eye
- .
4. repmat
- .
5. rand
- .
6. randn
- .
7. linspace - .
8. logspace - .
9. freqspace - .
10. meshgrid - (
11.
).
-
.
(Specialized matrices)
46. compan
- .
47. gallery - .
48. hadamard - .
49. hankel
- .
50. hilb
- .
51. invhilb - .
52. magic
- .
53. pascal
- .
54. rosser - (
).
55. toeplitz - (Toeplitz matrix).
56. vander - (Vandermonde matrix).
57. wilkinson - ( (Wilkinson)
.
6. -
(Matrix functions - numerical linear algebra)
matlab\matfun
(Matrix analysis)
1.
2.
3.
4.
5.
6.
7.
8.
9.
norm
- .
normest - 2- .
rank
- .
det
-
trace
- ( ).
null
- - () .
orth
- .
rref
- (Reduced row echelon form).
subspace - .
(Linear equations)
10. \ and /
- (. 3.
)
11. inv
- .
12. rcond
- , LAPACK (LAPACK reciprocal condition estimator).
13. cond
- .
14. condest - 1- .
15. normest1 - 1- .
16. chol
- (Cholesky factorization).
- .
- .
- .
- (
).
28. svds
- .
29. poly
- .
30. polyeig - (Polynomial
eigenvalue problem).
31. condeig - .
32. hess
- (Hessenberg form).
33. qz
- QZ- ( ).
34. schur
- (Schur decomposition).
(Matrix functions).
35. expm
36. logm
37. sqrtm
38. funm
39. expm1
40. expm2
41. expm3
- .
- .
- .
- .
- .
- .
-
.
(Factorization utilities)
42. qrdelete - QR -.
43. qrinsert - QR-.
44. rsf2csf - -
.
45. cdf2rdf - -
.
46. balance -
.
47. planerot - ( ).
48. cholupdate - .
49. qrupdate - QR .
pchip
- .
interp1 - .
interp1q - .
interpft -
.
5. interp2 - .
6. interp3 - .
7. interpn - .
8. griddata - .
9. griddata3 - .
10. griddatan - .
(Spline interpolation)
11. spline
12. ppval
- .
- - .
(Geometric analysis)
13. delaunay - (Delaunay triangulation).
14. delaunay3 - (3-D Delaunay tessellation).
15. delaunayn - (N-D Delaunay tessellation).
16. dsearch - .
17. dsearchn - .
18. tsearch - .
19. tsearchn - .
20. convhull - .
21. convhulln - .
22. voronoi - .
23. voronoin - .
24. inpolygon - , .
25. rectint - .
26. polyarea - .
(Polynomials)
27. roots
28. poly
- .
-
.
29. polyval - .
30. polyvalm - .
31. residue - ( ).
32. polyfit - .
33. polyder - .
34. polyint - .
35. conv
- .
36. deconv
- .
8.
(Data analysis and Fourier transforms)
matlab\datafun
(Basic operations)
1. max
- .
2. min
- .
3. mean
- .
4. median
- ( ).
5. std
- .
6. var
- .
7. sort
- .
8. sortrows - .
9. sum
- .
10. prod
- .
11. hist
- .
12. histc
- .
13. trapz
- .
14. cumsum
- .
15. cumprod - .
16. cumtrapz - .
(Finite differences)
17. diff
- .
18. gradient - .
19. del2
- .
(Correlation)
20. corrcoef - .
21. cov
- .
22. subspace - .
9. (Character strings)
matlab\strfun
(General)
1. char
- ().
2. double
- .
3. cellstr - .
4. blanks
- .
5. deblank - .
6. eval
- , .
(String tests)
7. ischar
8. iscellstr
9. isletter
10. isspace
- , .
- , .
- , () .
- , .
(String operations)
11. strcat
- .
12. strvcat - .
13. strcmp - .
14. strncmp - N .
15. strcmpi - .
16. strncmpi - N .
17. findstr - .
18. strjust - .
19. strmatch - .
20. strrep
- .
21. strtok
- , (token).
22. upper
- .
23. lower
- .
(String to number conversion)
24. num2str - .
25. int2str - .
26. mat2str - .
27. str2double - .
28. str2num - .
29. sprintf - .
30. sscanf
- .
-
.
32. hex2dec - .
33. dec2hex - .
34. bin2dec - .
35. dec2bin - .
36. base2dec - - .
37. dec2base - -.
MATLAB
PLOT
-
plot(Y)
plot(X1, Y1, ...)
plot(X1, Y1, LineSpec, ...)
plot(..., 'PropertyName', PropertyValue, ...)
h = plot(...)
. Y ,
plot(Y) Y
. Y , plot(Y)
plot(real(Y), imag(Y)). , plot .
plot(X1, Y1, ...), , .
, , X1 , Y1 .
X1 Y1 (
), Y1 X1.
Y1 , X1 ,
Y1 X1.
Y1 X1,
( Y1 - , )
X1, Y1 ( ),
X1 Y1 (. ).
plot(X1, Y1, LineSpec,...) ,
LineSpec , .
Xn, Yn, LineSpec Xn, Yn, .. LineSpec , : plot(X1, Y1, X2, Y2,
LineSpec, X3, Y3).
plot(... , 'PropertyName', PropertyValue,...) , 'PropertyName', ,
PropertyValue (. ).
h = plot(...) - (handles) -
, .
. ,
plot ,
ColorOrder . , ColorOrder, , plot (line styles) ,
LineStyleOrder . ,
, . ,
, MATLAB ColorOrder LineStyleOrder
plot.
, .
,
set(0, 'DefaultAxesColorOrder', [0 0 0], 'DefaultAxesLineStyleOrder', '-|-.|--|:')
()
( ColorOrder), (
LineStyleOrder) : (-), - (-.), (--)
(:).
. -
LineSpec.
1.
, ( line):
LineWidth (points), 1 1/72 .
MarkerEdgeColor
(, , , (), (), ).
MarkerFaceColor .
MarkerSize .
,
x = -pi : pi/10 : pi;
y = tan(sin(x)) - sin(tan(x));
plot(x, y, '--rs', 'LineWidth', 2,...
'MarkerEdgeColor', 'k',...
'MarkerFaceColor', 'g',...
'MarkerSize', 10)
2. (Tick-Mark) (Labeling)
,
. ,
x,
:
x = -pi : 0.1 : pi;
y = sin(x);
plot(x, y)
set(gca, 'XTick',-pi : pi/2 : pi)
set(gca, 'XTickLabel',{'-pi', '-pi/2', '0', 'pi/2', 'pi'})
,
-pi/4, sin(-pi/4).
x y,
xlabel('-\pi \leq \Theta \leq \pi')
ylabel('sin(\Theta)')
title('Plot of sin(\Theta)')
text -
text(-pi/4, sin(-pi/4), '\leftarrow sin(-\pi\div4)', 'HorizontalAlignment', 'left')
, , plot, Color property. -
LineWidth
set(findobj(gca, 'Type', 'line', 'Color', [0 0 1]),...
'Color','red',,...
'LineWidth', 2)
. :
axis, bar, grid, legend, line, LineSpec, loglog, plotyy, semilogx, semilogy, subplot, xlabel,
xlim, ylabel, ylim, zlabel, zlim, stem
. String text , .
LineSpec
. (Line).
. , -
. MATLAB , :
(Line style)
(Line width)
(Color)
(Marker type)
(Marker size)
( )
MATLAB () , .
.
-:
-.
( )
-
+
o
*
.
x
s
d
^
v
>
<
p
h
()
()
r
g
b
c
m
y
k
w
(cyan)
(magenta)
LineSpec,
:
, plot(x, y, '-.or') y x,
- (-.); (o) , , (r). (
) , .
LineSpec , (, plot(x,y,'d')), MATLAB
( )
() . plot plot3,
, :
LineWidth ( , 1/72 )
MarkerEdgeColor
(, , , , , ).
MarkerFaceColor
MarkerSize
, LineStyle, Color, Marker
. , ,
, ,
RGB.
ColorSpec.
,
, .
t = 0 : pi/20 : 2*pi;
plot(t, sin(t), '-.r*')
hold on
plot(sin(t - pi/2), '--mo')
plot(sin(t - pi), ':bs')
hold off
, .
plot(t, sin(2*t),'-mo',...
'LineWidth', 2,...
'MarkerEdgeColor', 'k',...
'MarkerFaceColor', [0.49 1 0.63],...
'MarkerSize', 12)
. :
line, plot, patch, set, surface, LineStyleOrder