Professional Documents
Culture Documents
MATLAB
MATLAB
Dr. Hammed
Computer Science
2008
MATLAB
MATLAB:
)(The MATLAB programming language
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~
-1 .MATLAB
-2 .
-3 .
-4 .
-5 .
-6 .
-7 .
-8 .
-9 .
-10 . MATLAB
-11 .
-12 .
-13 .
:
6.5 -1
MATLAB , ,
) (.
MATLAB Help Version 6.5 -2
www.mathworks.com -3
MATLAB
MATLAB
200
MATLAB .
MATLAB ,
.
C .FORTAN
) MATrix LABoratory
(
.
) toolboxes (
,
....
MATLAB
MATLAB
:
-1 MATLAB .
.double click
-2 start Programs
.MATLAB 6.5
start
Programs
MATLAB 6.5
MATLAB
) .(1
) (2
Windows .
Dr. Hammed
Computer Science
) :(1
MATLAB
) :(2
( )MATLAB
MATLAB
MATLAB
MATLAB
MATLAB MATLAB
.MATLAB
. MATLAB
MATLAB
MATLAB -:
-1
MATLAB
-4 MATLAB Start
. Ready
.
) (3
)(
Command Window
.
-5 View )
(3 MATLAB
-:
) :(3
View
- Command Window:
)<<(.
- :Workspace
MATLAB
) MATLAB
( , ).(4
6
MATLAB
)lists
(bar
- Launch Pad:
.
)tools
(bar
- Command History:
.
) (5 MATLAB ....
) :(5 MATLAB
MATLAB
:
-1 MATLAB MATLAB
).(scalar
-2 clear Workspace
.
-3 clc Workspace Window
.Command
-4 MATLAB :
,format short, long, short e, long e, hex, blank, +, rat
-5 ) demo (demonstration MATLAB
:
) Demos (Toolboxes
Help
-:
- .Demos
- ) .(Help -
. - Demo
Start
8
.Mfile
MATLAB
: Demos
-6 ) (...
.
-7
Command .Editor
-8 ) (%
:
% This Program Compute Area
-9 MATLAB Mfiles ) ,(m. )
.(examplel.m
-10 Command
.
-11 ) MATLAB (
Command Window .
-12 MATLAB
.
-13 ) (
:
Command
Help
MATLAB
) (Untitle1
.
) :(6 .
) :(9
.
10
) :(8 )
) :(7 )
(.
MATLAB
;clc
;clear
;tic
)(commands
;t = toc
-19
Help :
MATLAB Help
11
Help
MATLAB
-:
1- Contents.
2- Index.
3- Search.
4- Demos.
-20 break
) break (.
:
.
if .
.
else
.
;break
..
..
;break
{
}else
12
function .
.
.
.
;break
{
}
MATLAB
-21 return .
:
function
..
.
;return
-22
..
....
-23 .ans
-24
.
A, B, , Z, a, b, , z
9 , ,2 ,1 ,0
- ) , ( ,* , ; , > , < , =, - , + :
... , } ,.
Constants:
MATLAB -:
13
MATLAB
)( :Numerical Constants
:
:
) (1 :
: .
>> bitmax
= ans
9.007199254740991e+015
1-253
) (2 :
>> realmin
= ans
2.225073858507201e-308
>> realmax
= ans
1.797693134862316e+308
>> pi
= ans
3.146
) (3 : 10N
=i=j
:1 :
c = -7.7782 4.9497i
)cr = -7.7782 (c
14
cr = real
MATLAB
ci = imag
ci = -4.9497
)(c
)c2 = 3 * (2 sqrt (-1) * 3
6.000 9.000i:2
Numeric
)(
single
real
)(4 bytes
double
MATLAB
real
)(8 bytes
)
:1
(
;>> x = 100
;>> x = 100
;)>> x = uint8 (x
;)>> x = double (x
;>> y = x + 1
:2
;>> y = x + 1
Error
y = 101
)( String Constants:
""
quotations ' '
.
'= 'The speed of wind
''I love Basrah
''My birthday = 1970
15
MATLAB
A B :
''A' < 'B
)( Boolean Constants:
) (1 true ) (0 .false
:
1
3>2
0>5
Variables:
:
.1 ) (
:
if, end, for, break, else, global, return, function, sin, log,
.2 ) COST, CoST, cost, Cost
, A .( a
.3 ) l (small letter MATLAB 1.
.4 63
. 63
.5
. underscore .
.6 MATLAB ).( ,if, while, input
MATLAB:
)( Numerical Variables:
16
MATLAB
A Z a b
0 9
) (
underscore 63 .
) (.
:
Ali_Ahmed, X2, S2, ks, K
^ * / - +
.MATLAB
MATLAB
a 3b
a3*b
c2 - 10
c ^ 2 10
a2 + b2 / 12
(a ^ 2 + b ^ 2) / 12
)m * (7 * d 8 * g
)m (7d 8g
)( Rule of Precedence
,
, ) ( )
( :
:
A/B+C
C +
1
2
17
A
B
MATLAB
)A / (B + C
A
BC
1
2
A
.
:
M) ^ A - B / (K * F - X
1
2
3
4
5
.
: X M .
: K F .
:
.
: B .
: A .
Arithmetic Statement
MATLAB
MATLAB
18
MATLAB
) (
:
1( y = A * X + B
2) A = 3.14 * R ^ 2
:
:
Z = A B / C
1
2
3
.
)( String Variables:
) (.
String Statement
) (
.
:
;'A = 'Hameed AbdulKareem
;'N = 'Number of Student
;'Dept = 'Computer Science
:
' ' .
:Library Functions
19
MATLAB
MATLAB
:
Sqrt
abs
exp
log
log 10
log 2
sin
Cos
Tan
atan
fix
floor
ceil
round
mod
rem
Sign
imag
real
factor
Isprime
primes
gcd
lcm
10
2
, ,
true
:
;>> x = 2.6
;)>> y1 = fix (x); y2 = floor (x); y3 = ceil (x); y4 = round (x
y1 = 2
y2 = 2
20
MATLAB
y3 = 3
/
y4 = 3
:
.
)(d
sin (a + b) m / sqrt
1
3
4
:
: a .b
: ).(1
: .d
: m ).(3
: ) (4 ) (2
) (.
: :MATLAB
a 2 10
) b = sqrt ( a ^ 2 + 10
)z = ln (cx + ny
)z = log (c * x + n * y
)y = sin 3 (x + nk
y = (sin (x + n * k)) ^ 3
)s = tan-1 (y / x
)s = atan (y / x
r 2 e x 5
)) r = 2 * sqrt (exp (x 5
21
MATLAB
y
)( a m
g p 2 5 ab
)g = p ^ (3 / 2) + (a * b / c) ^ (1 / 5
.
.
. MATLAB
.
MATLAB
.
22
MATLAB
: )(1
):(2
0.9
]x = [1, 3, 7, 9, 20
)y = sin (x
0.8
0.7
0.6
0 x
0.5
0.4
0.3
0.2
0.1
y
0 0.31 0.59 0.81 0.95
1
0.95 0.81 0.59 0.31 0
x = [0 0.1 * pi .2 * pi .3 * pi .4 * pi .5 * pi .6 * pi .7 * pi .8 * pi .9 * pi
]pi
)y = sin (x
MATLAB
)(
. ) sin (x MATLAB
x y
MATLAB .
11
.
)>> x (3
= ans
0.6283
)>> y (5
= ans
0.9511
MATLAB
).(:
)>> x (1: 5
= ans
1.2566
23
0.9425
0.6283
0.3142
MATLAB
x 5 :1
1 .5
:
)>> x (7: end
= ans
3.1416
2.5133
2.8274
2.1991
1.885
end
.
:
)>> y (3: -1: 1
= ans
0
0.3090
0.5878
1 :1- :3
3 1 .1
:
)>> x (2: 2: 7
= ans
1.5708
0.9425
0.3142
x 7 :2 :2
2 2 .7
:
)]>> y ([8 2 9 1
= ans
0
0.5878
0.3090
0.8090
][1 9 2 8 y
24
MATLAB
. ]9 2 8
[1 .y
:
)]2
3 4
>> y ([1
= ans
0.3090
0.3090
0.8090
0.5878
:
MATLAB
.
)>> y (3.2
Error
)>> y (3.7
Error
)>> y (11.6
Error
x
x
111
x .
:
1) >> x = (0: 0.1: 1) * pi
) 2) >> x = linspace (0, pi, 11
:
25
MATLAB
]>> a = [1: 7
=a
7
:
])>> b = [linspace (1, 7, 5
=b
2.5 4
5.5 7
:
)>> a = (1: 7
=a
7
:
b = 1: 2: 9
>> a = 1 :5
=a
1
=b
1
:
:
]>> c = [b a
=c
4 5
c b .a
26
MATLAB
.
:
]5
>> c = [1
=c
:
]>> c = [1; 2; 3; 4; 5
=c
) (
1
2
3
4
5
:
>> a = 1: 5
=a
5
:
'>> b = a
)( a
.b
=b
1
2
3
4
5
27
MATLAB
:
) (
;>> k = b
:
]8
>> g = [1
=g
:
3 4
>> g = [1
7 8
]10 11 12
>> h = [1
Error
:
; )half = g (2, 2
; full = g
:
]>> c = [1: 5; 2: 2: 10; 7: -1: 3
=c
1 2 3 4 5
2 4 6 8 10
7 6 5 4 3
28
MATLAB
)>> c (1, 2
= ans
2
:
MATLAB
)
(.
)>> c (12
= ans
4
.
:
g %
>> g 2
= ans
2
-1
9 10
g .2
:
>> 2 * g 1
= ans
g , 2
9 11 13 15
1.
29
MATLAB
17 19 21 23
:
>> 2 * g / 5 + 1
= ans
2.6
2.2
1.8
1.4
4.2
3.8
3.4
5.8
5.4
4.6
g 2
5 .
. ,
,
MATLAB :
%
>> g
=g
9 10 11 12
]>> h = [1 1 1 1 ; 2 2 2 2 ; 3 3 3 3
=h
1 1 1 1
2 2 2 2
3 3 3 3
>> g + h
30
MATLAB
= ans
5
10
12 13 14 15
>> ans h
= ans
4
10 11 12
>> 2 * g h
= ans
3
10 12 14
15 17 19 21
)>> 2 * (g h
= ans
4
10 12
12 14 16 18
,
.
:
>> g .* h
= ans
4
31
MATLAB
14 16
10 12
33 36
27 30
g h
.
: MATLAB
,
.
>> g * h
Error
g
:
>> g ./ h
= ans
4.0000
3.0000
2.0000
1.0000
4.0000
3.5000
3.0000
2.5000
4.0000
3.6667
3.3333
3.0000
: , MATLAB
. ,
.
>> g .^ 2
= ans
16
64
49
25 36
32
MATLAB
MATLAB ,
,
,
.
) (
)>> ones (3
= ans
1 1 1
1 1 1
1 1 1
) (
= ans
) (
)>> size (g
= ans
3 4
))>> ones (size (g
= ans
1
: ) ones (n zeros
) (n MATLAB n n
.
33
MATLAB
) (
)>> eye (4
= ans
0
) (
)>> rand (3
= ans
0.4565
0.4860
0.9501
0.0185
0.8913
0.2311
0.8214
0.7621
0.6068
)>> rand (1, 5
= ans
0.7382
0.9218
0.7919
0.6154
0.4447
:
;>> d = pi
)>> d * ones (3, 4
= ans
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
( d (3 4
34
MATLAB
= ans
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
3.1416
: d .
MATLAB ,
,
,
,
. MATLAB
:
]>> A = [1 2 3; 4 5 6; 7 8 9
=A
3
>> A (3, 3) = 0
=A
) (3 ,3.
>> A (2, 6) = 1
=A
) (6 ,2 1
A ,
.
35
1 2 3 0 0 0
1
4 5 6 0 0
MATLAB
7 8 0 0 0
>> A (:, 4) = 4
=A
1 2 3 4 0 0
4 5 6 4 0
7 8 0 4 0
4 5 6 4 0
7 8 0 4 0
]>> A (:, 4) = [4 4 4
Error
:
]>> B = [7 8 9; 4 5 6; 1 2 3
=B
]9
>> A = [1 2 3; 4 5 6; 7 8
=A
3
])]>> C = [A B(:, [1 3
=C
C A
.B
36
MATLAB
)>> B = A (1: 2, 2: 3
=B
3
: B A
.
)>> B = A (:
=B
1
4
7
2
5
8
3
6
9
:
;]>> B = [1 2 3; 4 5 6; 7 8 9
)>> B = reshape (A, 1, 9
=B
1 4 7 2 5 8 3 6 9
;)]>> B = reshape (A, [1 9
=B
1 4 7 2 5 8 3 6 9
37
MATLAB
3*3 9*1
:
>> A = B
=A
3
] [ = )>> B (:, 2
B
=B
B ,
] [ ,
: )( . reshape
'>> C = B
=C
7
: reshape
). (transport
: .C
] [ = )>> C (2, :
=C
7
38
MATLAB
: A .C
>> A (2, :) = C
=A
3
:
>> x = -3: 3
=x
2 3
-1
-3
-2
.
, ) (1 true
) (0 false
. :
>> abs (x) > 1
= ans
1
-2
-3
,
:
]-4
-3; 2
>> B = [5
=B
39
MATLAB
-3
-4
)>> y = B (x
=y
5
-3
-4
, sort ,MATLAB
:
<< = x
) (
)randperm (8
=x
8
=y
8
1
= indx
40
MATLAB
:
) (
])>> A = [randperm (6); randperm (6); randperm (6); randperm (6
=A
3
= idx
, . MATLAB
41
MATLAB
,find
,true :
>> x = -3: 3
=x
3
-1
-3
-2
=k
6
)>> y = x (k
=y
3
-3
-2
-3
-2
find )
( ,:
]9
;6
;3
>> A = [1
=A
3
MATLAB
1
2
3
: diag .
9
9
6
9
9
8
9
8
8
5
8
7
7
=A
)>> diag (A
= ans
7
8
8
9
:
MATLAB max min
.
:
)>> v = rand (1, 6
=v
0.5417
0.3028
0.6822
0.1934
0.1897
0.3046
)>> max (v
= ans
0.6822
MATLAB
4
)>> min (v
= ans
0.1897
)>> [mn, j] = min (v
= mn
0.1897
=j
2
:
)>> A = rand (4, 6
=A
0.3704
0.7271
0.3420
0.8216
0.8537
0.1509
0.7027
0.3093
0.2897
0.6449
0.5936
0.6979
0.5466
0.8385
0.3412
0.8180
0.4966
0.3784
0.4449
0.5681
0.5341
0.6602
0.8998
0.8600
0.7027
0.8385
0.5341
0.8216
0.8600
=r
:
) (
;)'>> max (A
)>> [mn, r] = min (A
= mn
0.3704
0.3093
0.2897
0.6449
0.4966
0.1509
=r
44
MATLAB
:
) ( ;)'>> min (A
. :
)>> mmx = max (mx
= mmx
0.8998
))>> [mmx, i] = max (A (:
= mmx
0.8998
=i
8
: :
;))>> z = max (max (A
;))>> z = min (min (A
: .sum
;))>> z = sum (sum (A
,MATLAB
, ,
:
]9
6; 7
3; 4
>> A = [1
=A
45
MATLAB
up-down
)>> flipud (A
= ans
9
left-right
)>> fliplr (A
= ans
1
)(upper
)>> triu (A
= ans
3
)(lower
)>> tril (A
= ans
0
)(
;)>> g = det (A
)(
;)>> h = inv (A
;)>> i = eig (A
)>> j = eye (3
=j
46
MATLAB
;)>> trace (A
,
MATLAB length size numel :
]8
4; 5
>> A = [1
=A
)>> S = size (A
=S
4
) (2 )
.(4
)>> [r, c] = size (A
=r
2
=c
4
)>> r = size (A, 1
=r
2
)>> c = size (A, 2
=c
4
47
MATLAB
numel
:
)>> numel (A
= ans
8
length ,
:
)>> length (A
= ans
4
>> B = -3: 3
=B
3
-1
-2
-3
)>> length (B
= ans
7
A
: .
;]4
;]x .^ 4
48
2; 3
>> x = [1
x .^ 2; x .^ 3
>> y = [x
MATLAB
. MATLAB ) )n-D
arrays
. , ) ,(pages
,
,
.
,
.
, :
)>> A = zeros (4, 3, 2
= )A (:, :, 1
0
49
MATLAB
= )A (:, :, 2
,
.
:
;)>> B (:, :, 1) = zeros (2, 3
;)>> B (:, :, 2) = ones (2, 3
;>> B (:, :, 3) = 4
>> B
= )B (:, :, 1
0
= )B (:, :, 2
= )B (:, :, 3
reshape
:
])>> C = [B (:, :, 1), B (:, :, 2), B (:, :, 3
50
MATLAB
=C
4
= )ans (:, :, 2
= )ans (:, :, 3
size .
)>> [r, c, p] = size (C
=r
2
=c
3
=p
3
,
ndims :
)>> ndims (C
= ans
3
51
MATLAB
)>> numel (C
= ans
18
Cell Arrays
MATLAB
,
.
,
) ) ((
, .
, cell .
.
, } { ,
MATLAB ,
:
;}]9
8
52
6; 7
3; 4
MATLAB
;}>> A (1, 2) = {2 + 3i
;}'>> A (2, 1) = {'Ali Ahmed
;}>> A (2, 2) = {12: -2: 0
>> A
=A
][2.0000 + 3.0000i
][3 3 double
][1 7 double
''Ali Ahmed
MATLAB 2 2
,
,
.
) cell
, (indexing .
:
}A (i, j) = {x
x
A {i, j} = x
MATLAB
)(i, j .A
][1
}>> y {1
= ans
1
53
MATLAB
>> y {2}
ans =
hello
>> y {3}
ans =
0
:
>> ce = {[1
3; 5
>> ce {1}(2, 2)
ans =
6
:
>> x = rand (3, 3);
>> y = rand (3, 3);
>> z = rand (3, 3);
>> w {1} = x;
>> w {2} = y;
>> w {3} = z;
>> w
ans =
[3 3 double]
[3 3 double]
[3 3 double]
:
MATLAB
.
.
.
;)>> x {9} = rand (3, 3
}>> x {1
= ans
0.6813
0.6721
0.8462
0.3795
0.8381
0.5252
0.8318
0.0196
0.2026
)(
)<< x {1} (2, 2
= ans
0.8381
: .
;)>> L = length (x
;>> sum1 = 0
>> for i = 1: L
;}b = x {i
;sum1 = sum1 + b
;end
celldisp MATLAB
, :
)>> celldisp (A
= )A (1, 1
55
MATLAB
= )A (2, 1
Ali Ahmed
= )A (1, 2
2.0000 + 3.0000i
= )A (2, 2
0
10
12
,
,:
}>> A {2, 2
= ans
0
10
12
)>> A (2, 2
= ans
][1 7 double
)>> A (1, :
= ans
][2.0000 + 3.0000i
][3 3 double
,ans
.
,
,
: .
}2], 'John Smith'; 2 + 3i, 5
>> B = {[1
=B
56
MATLAB
''John Smith
][1 2 double
][5
][2.0000 + 3.0000i
,
, cell ] [
:
)>> C = cell (2, 3
=C
][
][
][
][
][
][
, :
'>> C (1, 1) = 'The does n't work
Error
,
,
.
}'>> C (1, 1) = {'The does n't work
=C
] [ ] [ ''The does n't work
][
][
][
57
][
][
][
MATLAB
,
MATLAB .
, .
, , :
>> A
=A
][2.0000 + 3.0000i
][3 3 double
][1 7 double
''Ali Ahmed
>> B
=B
''John Smith
][1 2 double
][5
][2.0000 + 3.0000i
) (
]>> C = [A; B
=C
][2.0000 + 3.0000i
][3 3 double
][1 7 double
''Ali Ahmed
''John Smith
][1 2 double
][5
][2.0000 + 3.0000i
:
)>> D = C ([1 3], :
=D
][2.0000 + 3.0000i
58
][3 3 double
MATLAB
][1 2 double
''John Smith
.
] [ = )>> C (3, :
=C
][2.0000 + 3.0000i
][3 3 double
][1 7 double
''Ali Ahmed
][5
][2.0000 + 3.0000i
reshape ,
, :
;)>> x = cells (3, 4
)>> size (x
= ans
4
reshape ,
size .
repmat
.
:
>> y
=y
59
][
][
][
][
][
][
MATLAB
][
][
][
][
][
][
][
][ ][
][
][
][
][
][ ][
][
][
][
][
][ ][
][
][
][
][
][ ][
][
][
][
][
][ ][
][
][
][
][
][ ][
][
][
MATLAB ,
.
MATLAB
ASCII ,
:
'?>> t = 'How about this character string
60
MATLAB
=t
?How about this character string
)>> size (t
= ans
32
Class
1
>> whos
Bytes
Size
Name
double array
16
1 2
ans
character array
64
1 32
116
32
116
111 117
97 98
32
111 119
72
Columns 13 through 24
114
116 101
99
97
99 104 97 114
32
105 115
Columns 25 through 32
114 105 110 103 63
116
115
32
)>> char (u
= ans
MATLAB
))>> char (u (1
= ans
H
,
,MATLAB
:
)>> u = t (16: 24
=u
character
, 16 24
character
)>> u = t (24: -1: 16
=u
retcarahc
character .
')>> u = t (16: 24
=u
c
h
a
r
a
c
t
e
r
character )(.
62
MATLAB
:
;' >> u = 'Hameed
;'>> v = 'Aiad
]>> w = [u v
=w
Hameed Aiad
disp
:
)>> disp (u
Hameed
) ( ,
,
:
' >> v = ['character strings having more than
''one row must have the same number
!'of columns just like arrays
]'
=v
character string having more than
one row must have the same number
!of columns just like array
char
, :
)'>> legends = char ('Wilt', 'Russel', 'Kareem', 'Bird', 'Magic', 'Jordan
= legends
Wilt
Russel
Kareem
63
MATLAB
Bird
Magic
Jordan
)>> size (legends
= ans
6
. MATLAB
num2str int2str fprintf
, :
)>> int2str (35
= ans
35
)>> class (ans
= ans
char
)>> num2str (3.5
= ans
3.5
)>> class (ans
= ans
char
))>> fprintf ('% 4.3f\n', sqrt (2
1.414
))>> size (fprintf ('% 4.3f\n' , sqrt (2
= ans
64
MATLAB
1
:
>> S = str2num ('3.5')
S=
3.5
>> t = ['3.5'
t=
3.5 sqrt(2)
1.5
9.5
1.4142
1.5000
9.5000
>> t = '[3.5sqrt(2);1.59.5]'
65
MATLAB
=t
][3.5sqrt(2);1.59.5
)>> str2num (t
= ans
1.4142
3.5000
9.5000
1.5000
findstr .
;'>> b = 'Peter Piper picked a peck of pickled peppers
)'' >> findstr (b,
= ans
37
29
26
19
21
12
40
38
= ans
30
22
13
,
,
,
, .
, :
}'?>> C = {'How'; 'about'; 'this for a'; 'cell array of strings
=C
66
MATLAB
''How
''about
''this for a
'?'cell array of strings
)>> size (C
= ans
4
} { ,
, C
,
.
, :
)>> C (2: 3
= ans
''about
''this for a
)]1
>> C ([4
= ans
MATLAB
, )C (indices
.
:
}>> S = C {4
=S
?cell array of strings
)>> size (s
= ans
1 22
:
)>> C {4} (1: 10
= ans
cell array
char ,
:
)>> S = char (C
=S
How
about
this for a
?cell array of strings
)>> size (S
= ans
22
cellstr
:
68
MATLAB
)>> cellstr (S
= ans
''How
''about
''this for a
'?'cell array of strings
:
69
MATLAB
:input -1
:(1)
>> x = input ('enter x: ')
enter x:
. :(2)
n = input ('enter n:');
m = input ('enter m:');
for i = 1: n
for j = 1: m
result (i, j) = i ^ j;
end;
end;
. :(3)
clc;
clear;
z = input ('enter name', 's');
string
:( )
-2
:
70
MATLAB
:
-1 :disp
):(1
;>> d = 15
;)>> disp (d
15
):(2
;'>> a = 'ali
;)>> disp (a
ali
):(3
;>> sum = 9.8
;)])>> disp (['sum = ', num2str (sum
sum = 9.8
):(4
;)'>> disp ('computer
computer
):(1
disp
) (.
):(2
71
MATLAB
disp
) ] [ ) ).(((3
-2 :msgbox
)'>> msgbox ('ok', 'result
)
(
) (3 :fprintf
):(1
;>> y = 1.2
;>> x = 100.5
;)>> fprintf ('variable x is % 6.3f\n', x
;)>> fprintf ('variable y is % 6.3f\n', y
variable x is 1.200
variable y is 100.500
6 3 .
):(2
;))>> fprintf ('% 8.3f\n', round (3.8
4.000
):(1
:
72
c%
d%
MATLAB
e%
f%
i%
o%
s%
x%
):(2
.Command Window
MATLAB
,
).(True/False
73
MATLAB
MATLAB
True ,False
True .False
,
.
) ( Relational Operators :
:
>
>=
<
<=
==
=~
)
=(
,
,
:
):(1
;>> a = 1; b = 5
>> x = a > b
=x
0
74
MATLAB
>> A = 1: 9, B = 9 - A
=A
9
4 5
0 1
=B
>> tf = A > 4
= tf
1
A ,4
,A 4 1 .A > 4
)>> tf = (A == B
= tf
0
A .B
:
)=( )==( , )==(
,
)=( .
) :(1 ) (thr
) .(thr
;]2
17
22
1 0
) :(2 ) (thr
) .(thr
75
MATLAB
17
22
10
) (:Logical Operators
,
MATLAB:
&
|
~
:
) AND(
)(
OR
)(
NOT
;>> a = 1
;>> b = 5
>> x = a ~= b
=x
1
)>> b = (1 == 1) & (2 ~= 3
=b
1
)>> b = (1==1) | (2 ~= 3
=b
1
))>> b = (1==1) & not ((2 ~= 3
=b
0
;>> A = 1: 9; B = 9 - A
76
MATLAB
>> tf = A > 4
= tf
1
A 4
)>> tf = ~ (A > 4
= tf
0
, .
)>> tf = (A > 2) & (A < 6
= tf
0
A 2 .
6
MATLAB
,
,
.
.MATLAB
) (
)'( , )^(.^ ,
)~(
) * ,(.* , )(./ /
) ,(+ )(-
)(:
)>( , )>=( ,
)<(,
)<=( ,
77
)==(, )=~(
)&( AND
)|( OR
MATLAB
IF-ELSE-END
. MATLAB if-else-end
:
if expression
)(commands
end
) (commands if end
) (expression .true :
;>> x = 10
>> if x == 10
)'disp ('ok
;end
, if-else-end :
if expression
)(commands evaluated if True
78
MATLAB
else
(commands evaluated if False)
end
expression
.false expression true
: if-else-end ,
if expression1
(commands evaluated if expression1 is true)
elseif expression2
(commands evaluated if expression2 is true)
elseif expression3
(commands evaluated if expression3 is true)
elseif expression4
(commands evaluated if expression4 is true)
.
.
.
else
(commands evaluated if no other expression is true)
end
:
:(1)
>> x = 10;
>> if x == 10
msgbox ('ok', 'result');
:(2)
>> if x == 10
79
MATLAB
SWITCH-CASE
: switch-case ,
switch expression
case test-expression1
(commands1)
case test-expression2
(commands2)
otherwise
(commands3)
end
80
MATLAB
expression .
expression test-expression1
case . , )(commands1
.end ,
, expression
test-exoression2 case . ,
((commands2 .end
case ,expression ((commands3
.otherwise
switch-case
switch-case :
):(1
;x = 1
switch x
}case {1, 2, 3, 4, 5
;)'disp ('1..5
}case {9, 10
;)'disp ('9..10
otherwise
;)'disp ('this is impossible
;end
5..1
):(2
;clc
;clear
;n = 3
switch n
81
MATLAB
case {0}
m = n + 3;
case {2}
m = 'ali';
case {3}
m = magic (n);
otherwise
disp ('error');
end;
disp (m);
2
:(3)
x = 2.7;
units = 'm';
switch units
case {'inch', 'in'}
y = x * 2.54;
case {'meter', 'm'}
y = x / 100;
case {'feed', 'ft'}
y = x * 2.54 / 12;
case {'millimeter', 'mm'}
y = x * 10;
case {'centimeter', 'cm'}
82
MATLAB
;y = x
otherwise
;)]units
y = 0.027
MATLAB :
for
83
MATLAB
for
, for :
for i = x1: x3: x2
)(commands
;end
) (commands for end
x1 x2 .x3 :
:
>> for n = 1: 10
;)x (n) = sin (n * pi / 10
;end
>> x
=x
Columns 1 through 7
0.9511 0.8090
0.8090
0.9511 1.0000
0.3090
0.5878
Columns 8 through 10
0.0000
0.5878
0.3090
:
n 1 10
end , n ,n = 1
n = 2 .n = 10
: 10 ).(10..1
)>> array = randperm (10
= array
5
10
MATLAB
;end
>> x
=x
Columns 1 through 7
0.9511 0.8090
0.9511 1.0000
0.5878
0.8090
0.3090
Columns 8 through 10
0.3090
0.0000
0.5878
n ) (1 ) (10 .array
:
for , :
>> for n =1: 5
for m = 5: -1: 1
;A (n, m) = n ^ 2 + m ^ 2
;end
;)disp (n
;end
1
2
3
4
5
>> A
=A
85
26
17
10
29
20
13
MATLAB
10 13 18
25
34
17 20 25
32
41
26 29 34
41
51
:
>> for i = 1: 10
disp (i);
end;
1
2
3
.
.
10
>> for i = 0: 2: 10
end;
disp (i);
0
2
4
6
8
10
10
86
MATLAB
8
6
4
2
>> for i =1: 10
for j = 1: 10
) (
;mult (i, j) = i * j
;end
10
20
9
18
10 12 14 16
24
27 30
4
8
12 15 18 21
32
3 6 40
;end
3
12 16 20 24 28
90 100
80
10 20 30 40 50 60 70
WHILE
while
for ,
while :
while expression
)(commands
;end
) (commands while end
expression ) ,(true
expression .
):(1
87
MATLAB
;>> x = 1
>> while x < 25
;)disp (x
x=x+
;1
;end
1
2
3
.
.
24
):(2
;>> num = 0; EPS = 1
>> while (1 + EPS) > 1
;EPS = EPS / 2
;num = num + 1
;end
>> num
= num
53
:
for while:
) for (while
;s = 0
;s = 0
;x = 1
for i = 1: 100
;s = s + i
;s = s + x
if s > 250
88
MATLAB
;break
if s > 250
;end
;break
;end
;end
;x = x + 5
;end
x = 51
i = 22
s = 286
s = 253
:
break
.
MATLAB
,MATLAB
,MATLAB save :
>> save
89
MATLAB
) (Workspace
matlab.mat .
, ,MATLAB
, , MAT-files
,
, MATLAB
.
save :
var3
var2
var1
>> save
var2
var1
filename
>> save
matlab.mat ,
.
) (MAT-file
:
>> load filename var1, var2, var3
filename.mat var1, var2, var3
.
90
MATLAB
91
MATLAB
MATLAB
,
, :
;)>> a = rand (2, 5
;)>> b = rand (2, 5
)>> isequal (a, b
= ans
0
)>> isequal (a, a
= ans
1
unique
:
]>> a = [2: 2: 8; 4: 2: 10
=a
8
10
)>> unique (a
= ans
2
4
6
8
10
ismember :
>> a = 1: 9
92
MATLAB
=a
9
>> b = 2: 2: 9
=b
6
union .
)>> union (a, b
= ans
9
intersect .
)>> intersect (a, b
= ans
8
setdiff .
)>> setdiff (a, b
= ans
9
:
.
93
MATLAB
,
.
)>> bitand (3, 4
= ans
0
)>> bitor (3, 4
= ans
7
)>> bitxor (13, 27
= ans
22
20
30 1
30
31
)(
0
)>> bitshift (3, 2
= ans
12
)(
;]9
94
4 ; 3
>> z = [7
MATLAB
MATLAB
bin2dec,dec2bin :
)>> a = dec2bin (17
= ans
10001
)>> class (a
= ans
char
)>> bin2dec (a
= ans
17
)>> class (ans
= ans
double
)>> dec2bin (17, 6
= ans
01001
)
(16 hex2dec dec2hex :
)>> a = dec2hex (2047
=a
95
MATLAB
7FF
)>> dec2hex (2047, 4
= ans
07FF
)>> class (a
= ans
char
)>> hex2dec (a
= ans
2047
)>> class (ans
= ans
double
.MATLAB :
96
MATLAB
:
:(1)
()
result = sum1 / L;
>> avgx
avgx =
4.5000
>> avgy
avgy =
19.5000
:(2)
97
MATLAB
result = 2 * x;
()
>> res1
res1 =
2
10
12
14
16
>> res2
res2 =
22
24
26
18
140
:(3)
x2 = cos (x);
()
x3 = 2 * x;
>> plot (sin_x)
98
MATLAB
:
-1
.
-2 )=(
.
:
;)function y = myfunction (a, b
------------------ myfunction.m
-3 ).(end
-4 .
-5 .
-6 .
MATLAB
,
, .
plot
99
MATLAB
.
):(1
;]x = [1: 0.5: 10
) y( );y = exp (x
x, y
)plot (x, y
):(2
;x = 1: 10
)plot (x
:
) ( plot
) (x, x .
):(3
;] [ = y
for i = 1: 10
;)y (i) = exp (i
;end
;)plot (y
):(4
;] [ = y
for i = 1: 10
;])y = [y exp (i
;end
;)plot (y
):(5
(graph).
;clc
;clear
100
MATLAB
x = 0: pi / 100: 2 * pi;
y = sin (x);
plot (x, y);
legend ('sin (x)');
( )
:(6)
plot (x, sin (x), x, cos (x));
101
MATLAB
):(7
;)'* plot (x, sin (x), 'r: +', x, cos (x), 'b:
) cos (x )sin (x
:
:
;)'text (x, y, 'string
plot3
plot ,plot3
plot .
:
;)plot3 (x1, y1, z1, s1, x2, y2, z2, s2,
102
) (
MATLAB
:
;)t = linspace (0, 10 * pi, 100
;)plot3 (sin (t), cos (t), t
;)')xlabel ('sin (t
;)')ylabel ('cos (t
;)'zlabel ('t
;)'text (0, 0, 0, 'origin
grid on
figure ,
) subplot (m, n, p m*n
p .
, ,
:
:
;)x = linspace (0, 2 * pi, 30
;)y = sin (x
;)z = cos (x
103
MATLAB
2 * pi
-1
2 * pi
-1
2 * pi
-1
2 * pi
-20
: surf ,
:(1)
[x
z] = peaks (30);
MATLAB
):(2
for i = 1: 10
for j =1: 10
;mult (i, j) = i * j
;end
;end
)
)surf (mult
:
:
bar
bar chart
:
;x = -2.9: 0.2: 2.9
;))bar (x, exp (-x .* x
105
MATLAB
hist
histogram
:
x = -2.9: 0.1: 2.9;
y = randn (10000, 1);
hist (y, x);
pie
pie chart
:
x = [1
0.5
explode = [0
2.5
0
2];
0
0];
106
MATLAB
. :
clear;
clc;
corr = [0.0012, 0.0208, 0.0633, 0.1391];
amount = [1, 2, 3, 4];
subplot (211);
plot (amount, corr, '--rs');
title ('Cipher-image VS Amount of Encrypted Data');
107