Professional Documents
Culture Documents
Dr. Hammed
Computer Science
8002
MATLAB
:MATLAB
()The MATLAB programming language
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 1 .MATLAB
- 2 .
- 3 .
- 4 .
- 5 .
- 6 .
- 7 .
- 8 .
- 9 .
- 11 .MATLAB
- 11 .
- 12 .
- 13 .
:
6.5 - 1
MATLAB , ( , ).
MATLAB
MATLAB ,
,
,
211 , ,
,MATLAB
.
MATLAB ,
.
,
C .FORTAN
( MATrix LABoratory ) ,
.
( toolboxes ) ,
,
, ,
....
MATLAB
) (GUI .
MATLAB
MATLAB
:
- 1 MATLAB .
.double click
Programs
MATLAB 6.5
MATLAB
( .) 1
( )2 Windows
.
Dr. Hammed
Computer Science
( :)2 )
(MATLAB
( :)1 MATLAB
MATLAB
MATLAB
MATLAB MATLAB
, MATLAB
.MATLAB ,
.MATLAB
MATLAB
MATLAB - :
- 1
MATLAB
( :)3 View
- :Command Window
(>>).
- :Workspace
MATLAB (
MATLAB ) , (.)4
MATLAB
- :Help ()
.
- :Launch Pad
.
- :Command History
.
( )5 MATLAB ....
)(lists bar
)(tools bar
( :)5 MATLAB
MATLAB
:
- 1 MATLAB , MATLAB
(.)scalar
- 2 clear Workspace .
- 3 clc Workspace .Command Window
- 4 MATLAB :
format short, long, short e, long e, hex, blank, +, rat,
- 5 ( demo )demonstration MATLAB :
( )Toolboxes
Help
Demos
- :
- .Demos
- ( .)Help -
. - Demo
: Demos
.Mfile
Start
- 6 ( )...
.
- 7 Command
.Editor
- 8 ( )% ,:
% This Program Compute Area
- 9 MATLAB Mfiles ( ,).m (.)examplel.m
- 11
Command
.
- 11
( MATLAB ) Command
Window .
- 12
MATLAB
MATLAB
- 13
( )
:
Command
Help
,M-file ()
Run
MATLAB
( :)7 ( ).
( :)6 .
( :)9 .
( :)8 ( ).
- 16
global .
- 18
( )tic ( )toc .
10
MATLAB
;clc
;clear
;tic
)(commands
;t = toc
- 19
Help :
MATLAB Help
Help
,- :
1- Contents.
2- Index.
3- Search.
4- Demos.
- 21 break ( (
break ).
11
MATLAB
:
.
if .
function .
.
.
else
;break
;break
..
} {else
;break
..
} {
- 21 return .
:
function
..
.
;return
..
....
- 22 .
- 23 .ans
- 24 .
12
MATLAB
A, B, , Z, a, b, , z
0, 1, 2, , 9
- :
:Constants
MATLAB - :
() : Numerical Constants
:
( )1 :
: .
>> bitmax
= ans
9.007199254740991e+015
253-1
( )2 :
>> realmin
= ans
2.225073858507201e-308
>> realmax
= ans
1.797693134862316e+308
>> pi
= ans
3.146
13
MATLAB
( )3 : 10N MATLAB
EN 103 2.0 2.0E3 : 2.0E+3 MATLAB
- 102 1.7 -1.7E2 : : 0.0032
10-3 3.2E-3 : 3.2
( )4 :
i = j = 1 :
:1 :
c = -7.7782 4.9497i
)cr = real (c
cr = -7.7782
)ci = imag (c
ci = -4.9497
:2
6.000 9.000i
Numeric ()
single
real
)(4 bytes
( )
:1
double
MATLAB
real
)(8 bytes
:2
;>> x = 100
;>> x = 100
;)>> x = uint8 (x
;)>> x = double (x
;>> y = x + 1
;>> y = x + 1
101
Error
() :String Constants
14
= y
MATLAB
quotations ' '
, .
'= 'The speed of wind
''I love Basrah
''My birthday = 1970
, , ,
,
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 )small letter( l MATLAB .1
.4 63 .63
. 5
.underscore .
15
MATLAB
^ ,* ,/ ,- ,
.MATLAB
MATLAB
a3*b
a 3b
c ^ 2 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
A / B + C
1
2
16
+ C
MATLAB
(A / )B + C
A
BC
1
2
A .
:
)A - B / (K * F - X ^ M
2
3
4
5
, .
: X M .
: K F .
: .
: B .
: A .
Arithmetic Statement
MATLAB MATLAB
( ) , :
17
MATLAB
1( y = A * X + B
2) A = 3.14 * R ^ 2
:
:
Z=AB/C
1
2
3
.
() :String Variables
( ).
String Statement
( ) .
:
;'A = 'Hameed AbdulKareem
;'N = 'Number of Student
;'Dept = 'Computer Science
:
' '.
:Library Functions
MATLAB ,
,:
18
MATLAB
Sqrt
abs
exp
10
log
log 10
log 2
sin
Cos
Tan
atan
fix
floor
ceil
round
mod
rem
Sign
, ,
imag
real
factor
Isprime
true
primes
gcd
lcm
19
MATLAB
:
;>> x = 2.6
;)>> y1 = fix (x); y2 = floor (x); y3 = ceil (x); y4 = round (x
y1 = 2
y2 = 2
y3 = 3
/
y4 = 3
:
.
)(d
sin (a + b) m / sqrt
1
3
4
:
: a .b
: (.)1
: .d
: m (.)3
: ( )4 ( )2 (
).
: :MATLAB
b 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
20
MATLAB
s = tan-1 (y / x)
s = atan (y / x)
r = 2 * sqrt (exp (x 5 ))
t = abs (x sqrt (y)) / (a + m)
g = p ^ (3 / 2) + (a * b / c) ^ (1 / 5)
21
r 2 e x 5
x y
gp
(a m)
3
5 ab
MATLAB
.
. ,
, ,
. , MATLAB
.
MATLAB ,
.
(:)1
(:)2
]x = [1, 3, 7, 9, 20
)y = sin (x
0 x
x
y
0
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
22
MATLAB
= ans
0.9511
MATLAB
(.):
)>> x (1: 5
= ans
1.2566
0.9425
0.6283
0.3142
,x 1: 5 1
.5
:
)>> x (7: end
= ans
3.1416
2.8274
2.5133
2.1991
1.885
, end
.
:
)>> y (3: -1: 1
= ans
0
0.3090
0.5878
, 3: -1: 1 3
1 .1
:
)>> x (2: 2: 7
= ans
1.5708
0.9425
0.3142
,x 2: 2: 7 2
2 .7
23
MATLAB
:
)]>> y ([8 2 9 1
= ans
0
0.5878
0.8090
0.3090
] [8 2 9 1 y ,
,
. ] [8 2 9 1 .y
:
2
)]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
24
MATLAB
:
]>> a = [1: 7
=a
7
:
])>> b = [linspace (1, 7, 5
=b
5.5 7
2.5 4
:
)>> 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
25
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
:
26
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
)>> c (1, 2
27
MATLAB
= 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
17 19 21 23
:
>> 2 * g / 5 + 1
28
MATLAB
= ans
2.6
2.2
1.4
1.8
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
= ans
5
10
12 13 14 15
>> ans h
29
MATLAB
= 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
14 16
10 12
33 36
27 30
g h
.
: MATLAB
, .
30
MATLAB
>> g * h
Error
g h
:
>> 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
MATLAB ,
, ,
.
( )
)>> ones (3
= ans
1 1 1
1 1 1
1 1 1
31
MATLAB
( )
= ans
( )
)>> size (g
= ans
3 4
))>> ones (size (g
= ans
1
: ) ones (n ) zeros (n
MATLAB n n .
( )
)>> 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
32
MATLAB
= 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
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
33
MATLAB
=A
3
>> A (3, 3) = 0
=A
) (3, 3.
>> A (2, 6) = 1
=A
) (2, 6 1 A
1 2 3 0 0 0
4 5 6 0 0
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
:
34
Error
MATLAB
]>> 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
)>> B = A (1: 2, 2: 3
=B
3
: B A .
)>> B = A (:
=B
1
4
7
2
5
35
MATLAB
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
3*3 1*9
:
>> A = B
=A
3
] [ = )>> B (:, 2
=B
B
B , ] [,
.
: ()
.reshape
'>> C = B
=C
36
MATLAB
: reshape
).(transport
: .C
] [ = )>> C (2, :
=C
7
: A .C
>> A (2, :) = C
=A
3
:
>> x = -3: 3
=x
2 3
-1
-2
-3
.
,
) (1 true ) (0 false
. :
>> abs (x) > 1
37
MATLAB
= ans
1
-3
-2
, :
]-4
-3; 2
>> B = [5
=B
-3
-4
)>> y = B (x
=y
5
-3
-4
,
sort ,MATLAB :
( )
)>> x = randperm (8
=x
8
38
MATLAB
1
= indx
4
:
( )
])>> A = [randperm (6); randperm (6); randperm (6); randperm (6
=A
3
= idx
39
MATLAB
,
. MATLAB ,find
,true :
>> x = -3: 3
=x
3
-1
-3
-2
=k
2
)>> y = x (k
=y
3
-3
-2
-3
-2
find ( ) ,:
]9
;6
;3
>> A = [1
=A
3
MATLAB
3
3
=j
1
2
3
: diag .
8 9 9
8 9 9
5 8 6
8 9 9
7
7
A=
4
)>> diag (A
= ans
7
8
8
9
:
MATLAB min , max
.
:
)>> v = rand (1, 6
=v
0.5417
0.3028
0.6822
0.1934
0.1897
0.3046
)>> max (v
= ans
0.6822
41
MATLAB
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
:
( )
42
;)'>> max (A
MATLAB
0.3093
0.2897
0.6449
0.1509
0.4966
=r
2
:
( )
;)'>> 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
3
43
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
44
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
45
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
46
2; 3
>> x = [1
x .^ 2; x .^ 3
>> y = [x
MATLAB
.
MATLAB (
(n-D arrays
. ,
) ,(pages ,
,
.
,
.
, :
)>> A = zeros (4, 3, 2
= )A (:, :, 1
0
= )A (:, :, 2
,
.
:
;)>> B (:, :, 1) = zeros (2, 3
47
MATLAB
= )B (:, :, 2
= )B (:, :, 3
reshape
:
])>> C = [B (:, :, 1), B (:, :, 2), B (:, :, 3
=C
4
= )ans (:, :, 2
= )ans (:, :, 3
48
MATLAB
size .
)>> [r, c, p] = size (C
=r
2
=c
3
=p
3
, ndims
:
)>> ndims (C
= ans
3
)>> numel (C
= ans
18
49
MATLAB
Cell Arrays
MATLAB ,
. ,
( ( ))
,
.
,cell
( .
.
MATLAB
, } { ,
, :
;}]9
6; 7
3; 4
;}>> 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 .
50
MATLAB
:
}A (i, j) = {x
A {i, j} = x MATLAB
x ) (i, j .A
][0
][1
}>> y {1
= ans
1
}>> y {2
= ans
hello
}>> y {3
= ans
0
:
;}7], 'yes', 3 > 2
3; 5
>> ce = {[1
)>> ce {1}(2, 2
= ans
6
:
;)>> x = rand (3, 3
;)>> y = rand (3, 3
51
MATLAB
][3 3 double
][3 3 double
:
;)>> x {1} = rand (3, 3
;)>> x {2} = rand (3, 3
;)>> x {3} = rand (3, 3
.
.
.
.
.
;)>> 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
MATLAB
;)>> L = length (x
;>> sum1 = 0
>> for i = 1: L
;}b = x {i
;sum1 = sum1 + b
;end
celldisp MATLAB ,
:
)>> celldisp (A
= )A (1, 1
3
7
= )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
53
MATLAB
)>> A (1, :
= ans
][2.0000 + 3.0000i
][3 3 double
,ans
.
,
, . :
}2], 'John Smith'; 2 + 3i, 5
>> B = {[1
=B
''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
][
54
][
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
55
MATLAB
:
)>> D = C ([1 3], :
=D
][2.0000 + 3.0000i
][3 3 double
''John Smith
][1 2 double
.
] [ = )>> 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
.
:
56
MATLAB
>> y
y=
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
57
MATLAB
MATLAB ,
.
MATLAB ASCII
,:
'?>> t = 'How about this character string
=t
?How about this character string
)>> size (t
= ans
32
1
>> whos
Class
Bytes
Size
Name
double array
16
12
ans
character array
64
1 32
116
32
116
117
111
58
97 98
32
119
111
72
MATLAB
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
r
a
c
t
e
r
character ().
:
;' >> 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
60
MATLAB
char ,
:
)'>> legends = char ('Wilt', 'Russel', 'Kareem', 'Bird', 'Magic', 'Jordan
= legends
Wilt
Russel
Kareem
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
61
MATLAB
ans =
char
>> fprintf ('% 4.3f\n', sqrt (2))
1.414
>> size (fprintf ('% 4.3f\n' , sqrt (2))
ans =
1
1
:
>> S = str2num ('3.5')
S=
3.5
>> t = ['3.5'
'sqrt(2)'
;'1.5' '9.5']
t=
3.5 sqrt(2)
1.5
9.5
MATLAB
= ans
1.4142
3.5000
9.5000
1.5000
']>> t = '[3.5sqrt(2);1.59.5
=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
,
, ,
,
.
63
MATLAB
, :
}'?>> C = {'How'; 'about'; 'this for a'; 'cell array of strings
=C
''How
''about
''this for a
'?'cell array of strings
)>> size (C
= ans
1
} { ,
, C ,
.
, :
)>> C (2: 3
= ans
''about
''this for a
)]1
>> C ([4
= ans
MATLAB
''How
))>> size (C (1
= ans
1
, ) 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
65
MATLAB
cellstr( :
)>> cellstr (S
= ans
''How
''about
''this for a
'?'cell array of strings
66
MATLAB
:
- 1 : input
(:)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 ( ):
:
;}'prompt = {'enter x
;}'def = {'20
;'dlgTitle = 'Input for my program
67
MATLAB
lineNo = 1;
% num string
:
: disp - 1
:)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(
68
MATLAB
disp (
).
(:)2
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
69
MATLAB
%c
%d
%e
%f
%i
%o
%s
%x
(:)2
.Command Window
70
MATLAB
MATLAB ,
).(True/False
MATLAB
True ,False
True .False
,
.
( ) Relational Operators :
:
<
=<
>
=>
==
( =)
=~
,
,
:
(:)1
;>> a = 1; b = 5
>> x = a > b
=x
71
MATLAB
0
>> 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
22
17
72
1 0
MATLAB
( :)2 ( )thr (
.)thr
)>> z = inddent .* (inddent > thr
=z
0
22
17
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
73
MATLAB
;>> A = 1: 9; B = 9 - A
>> tf = A > 4
= tf
1
( A 4
)>> tf = ~ (A > 4
= tf
0
, .
)>> tf = (A > 2) & (A < 6
= tf
0
A 2 .6
MATLAB(
, ,
.
.MATLAB
74
MATLAB
( )
( ') , (^).^ ,
(~)
( * ,).* , ()./ ,/
( ) , ( )-
():
(<) , (=<) , (>),
(=>) , (==), (=~)
(&) AND
(|) OR
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
75
MATLAB
MATLAB
>> if x == 10
msgbox ('ok', 'result');
else
msgbox ('no', 'result');
end;
:)3(
>> x = 11;
>> if x == 1
disp ('1');
elseif x == 2
disp ('2');
else
disp ('3');
end;
SWITCH-CASE
,
: switch-case
switch expression
case test-expression1
(commands1)
case test-expression2
(commands2)
otherwise
(commands3)
end
77
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
1..5
(:)2
;clc
;clear
;n = 3
switch n
}case {0
;m = n + 3
78
MATLAB
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'}
y = x;
otherwise
79
MATLAB
units]);
end;
y = 0.027
80
MATLAB
MATLAB :
for
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.8090
0.9511
1.0000
0.9511
0.8090
0.5878
0.3090
Columns 8 through 10
0.0000
0.3090
0.5878
:
n 1 10 end ,
n ,n = 1 n = 2 .n = 10
: 10 (.)1..10
)>> array = randperm (10
= array
81
MATLAB
10
0.9511
1.0000
0.9511
0.8090
0.5878
0.3090
Columns 8 through 10
0.0000
0.3090
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
82
MATLAB
A=
2
10
17
26
13
20
29
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
disp (i);
end;
0
2
4
6
8
10
MATLAB
;)disp (i
;end
10
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
12 15 18 21
32
12 16 20 24 28
80
10 20 30 40 50 60 70
24
27 30
3 6 40
90 100
;end
WHILE
while for
, while :
while expression
)(commands
;end
) (commands while end
expression ) ,(true expression .
84
MATLAB
:)1(
>> x = 1;
>> while x < 25
disp (x);
= x + 1;
end;
1
2
3
.
.
24
:)2(
85
MATLAB
:
for while:
( for )while
;s = 0
;s = 0
;x = 1
for i = 1: 100
;s = s + i
;s = s + x
if s > 250
if s > 250
;break
;end
;break
;end
;end
;x = x + 5
;end
x = 51
i = 22
s = 286
s = 253
:
break
.
86
MATLAB
MATLAB
,MATLAB
,MATLAB save :
>> save
) (Workspace
matlab.mat . ,
,MATLAB , ,
MAT-files ,
,MATLAB
.
save :
var3
var2
var1
>> save
var2
:
var3
var1
var2,
filename
>> save
var1,
.filename.mat
load save save
:
>> load
matlab.mat
.MATLAB
matlab.mat ,
.
) (MAT-file
:
87
MATLAB
88
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 :
89
MATLAB
>> a = 1: 9
=a
8
>> b = 2: 2: 9
=b
8
union .
)>> union (a, b
= ans
9
intersect .
)>> intersect (a, b
= ans
8
setdiff .
)>> setdiff (a, b
= ans
9
:
.
90
MATLAB
, (
.
)>> bitand (3, 4
= ans
0
)>> bitor (3, 4
= ans
7
)>> bitxor (13, 27
= ans
22
21
31 1
31
31
()
()
;]9
91
4 ; 3
>> z = [7
MATLAB
MATLAB
dec2bin ,bin2dec :
)>> 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
92
MATLAB
7FF
>> dec2hex (2047, 4)
ans =
07FF
>> class (a)
ans =
char
>> hex2dec (a)
ans =
2047
93
MATLAB
.MATLAB :
(:)1
;]>> x = [1, 2, 3, 4, 5, 6, 7, 8
()
;)L = length (z
;)sum1 = sum (z
;result = sum1 / L
>> avgx
= avgx
4.5000
>> avgy
= avgy
19.5000
94
MATLAB
:)2(
) (
>> res1
res1 =
2
10
12
14
16
>> res2
res2 =
22
24
26
18
140
:)3(
x2 = cos (x);
x3 = 2 * x;
>> plot (sin_x)
95
MATLAB
:
- 1
.
- 2 (=)
.
:
;)function y = myfunction (a, b
------------------ myfunction.m
- 3 ).(end
- 4 .
- 5 .
- 6 .
96
MATLAB
MATLAB
,
, .
plot
( .
(:)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
97
MATLAB
end;
plot (y);
:)5(
.(graph)
clc;
clear;
x = 0: pi / 100: 2 * pi;
y = sin (x);
plot (x, y);
legend ('sin (x)');
) (
:)6(
plot (x, sin (x), x, cos (x));
98
MATLAB
(:)7
;)'* plot (x, sin (x), 'r: +', x, cos (x), 'b:
) cos (x )sin (x
99
MATLAB
:
:
;)'text (x, y, 'string
plot3
( )
:
;)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
100
grid on
MATLAB
figure , subplot (m,
) n, p m*n p .
,
, :
:
;)x = linspace (0, 2 * pi, 30
;)y = sin (x
;)z = cos (x
;)a = 2 * sin (x) .* cos (x
;)b = sin (x) ./ (cos (x) + eps
;)subplot (2, 2, 1
;)'1]); title ('Figure1
-1
2 * pi
-1
2 * pi
-1
2 * pi
101
-20
2 * pi
MATLAB
,
surf :
(:)1
;)z] = peaks (30
[x
;)surf (x, y, z
;)'xlabel ('x-axis
;)'ylabel ('y-axis
;)'zlabel ('z-axis
(:)2
for i = 1: 10
for j =1: 10
;mult (i, j) = i * j
;end
;end
( (
:
:
102
)surf (mult
MATLAB
bar
bar chart
:
x = -2.9: 0.2: 2.9;
bar (x, exp (-x .* x));
hist
histogram
:
x = -2.9: 0.1: 2.9;
y = randn (10000, 1);
hist (y, x);
103
MATLAB
pie
pie chart
:
x = [1
0.5
explode = [0
2.5
0
2];
0
0];
. :
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');
104