You are on page 1of 104

MATLAB

Dr. Ali

1
MATLAB

:MATLAB

()The MATLAB programming language


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-1 .MATLAB
-2 .
-3 .
-4 .
-5 .
-6 .
-7 .
-8 .
-9 .
-10 .MATLAB
-11 .
-12 .
-13 .

2
MATLAB

MATLAB 6.5 -1 , ( , ).
MATLAB Help Version 6.5 -2
www.mathworks.com -3

The MATLAB Programming Language : MATLAB



MATLAB ,
,
,
200 , ,
,MATLAB
.
MATLAB ,
.
,
C .FORTAN
( MATrix LABoratory ) ,
.
( toolboxes ) ,
,
, ,
....

3
MATLAB

MATLAB Graphical User Interface


) (GUI .

MATLAB
:
-1 MATLAB .

.double click
-2 start Programs MATLAB
.6.5
start Programs MATLAB 6.5
MATLAB
( .) 1
( ) 2 Windows
.

Dr. Ali
Computer Science

4
MATLAB

MATLAB
MATLAB MATLAB
, MATLAB
.MATLAB ,
.MATLAB
MATLAB
MATLAB -:
-1

-2 ) (Menu Bar ) (Lists Bar ,File ,Edit
... ,View .Help
-3 ) (Tools Bar
.
)(Current Directory
( )2
() MATLAB6P5\work C:

5
MATLAB

-4 MATLAB Start
. Ready
.


( )3 () ,
,Command Window
.
-5 View ( )3
MATLAB -:

( :)3 View
- :Command Window
(>>).
- :Workspace
MATLAB (
MATLAB ) , (.)4

6
MATLAB

- :Current Directory
.MATLAB
- :Help ()
.
- :Launch Pad
.
- :Command History
.
( )5 MATLAB ....
)(lists bar

)(tools bar

7
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 :
Help Demos ( )Toolboxes
-:
- ( .)Help - - .Demos
.Mfile . - Demo
Start : Demos
-6 ( )...
.
-7 Command
.Editor
-8 ( )% ,:
% This Program Compute Area
-9 MATLAB Mfiles ( ,).m (.)examplel.m

8
MATLAB

Command -10
.
( MATLAB ) Command -11
Window .
MATLAB -12
.
( ) -13
:

Command

Help


Copy Command Window Paste.


,M-file () -14
,MATLAB New File
M-file Open .
( MATLAB ) .
(.)8( ,)7( ,)6

9
MATLAB

Run -15
Editor F5 Run ,Debug
>> .Command
() M-file ( )example1.m
Save File
.)MATLAB (.)9
:
) (Untitle1
.

( :)7 ( ). ( :)6 .

( :)9 .

10
( :)8 ( ).
MATLAB

MATLAB Exit MATLAB -16


File MATLAB Exit
,Command () MATLAB .
global . -17
( )tic ( )toc . -18
:
;clc
;clear
;tic
)(commands
;t = toc
Help : -19
Help MATLAB Help
,-:
1- Contents.
2- Index.
3- Search.
4- Demos.

11
MATLAB

-20 break (
break ).
:
function . if . .
. . .
. else .
. ;break ;break
;break } {else ..
} { ..

-21 return .
function :
..
.


;return
..

....

-22 .
-23 .ans
-24 .

12
MATLAB

MATLAB Symbols : MATLAB


MATLAB :
A, B, , Z, a, b, , z - ::
0, 1, 2, , 9 - :
( ... , {, + , - ,= , > , < , ; , *, ) ,. - :
:Constants
MATLAB -:
() :Numerical Constants
:
0, +23, 472, -18 : ( )1 :
: .
>> bitmax
= ans
9.007199254740991e+015
253-1

13
MATLAB

0.0, 51.8, 472.5, -18.0 : ( )2 :


>> realmin :
= ans
2.225073858507201e-308
>> realmax
= ans
1.797693134862316e+308
>> pi
= ans
3.146
( )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
sqrt (-2) , 6 + sin (0.5) * j , 6 - 9i , 1 - 2i : ( )4 :
i = j = 1 :
c = -7.7782 4.9497i :1 :
cr = -7.7782 )cr = real (c
ci = -4.9497 )ci = imag (c
)c2 = 3 * (2 sqrt (-1) * 3 6.000 9.000i :2

:2 :1
;>> x = 100 ;>> x = 100
;)>> x = double (x ;)>> x = uint8 (x
;>> y = x + 1 ;>> y = x + 1
14
MATLAB

= y 101 Error
() :String Constants
""
quotations ' '
, .
'= 'The speed of wind
''I love Basrah
''My birthday = 1970
, , ,
,
A B
:
''A' < 'B
() :Boolean Constants
) (1 true ) (0 .false
:
3>2 1
0>5 0
: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
15
MATLAB

. 5
.underscore .
.6 MATLAB ) .(if, while, input,
MATLAB:
() :Numerical Variables
A Z a b
0 9 (
) underscore 63 .
( , , ).
:
Ali_Ahmed, X2, S2, ks, K


^ ,* ,/ ,- ,
.MATLAB
MATLAB
a3*b a 3b
c ^ 2 10 c2 - 10
(a ^ 2 + b ^ 2) / 12 a2 + b2 / 12
)m * (7 * d 8 * g )m (7d 8g
() Rule of Precedence
,
,
, , ( ) ,
( ) :
:

16
MATLAB

A
+ C A / B + C
B

2
A
(A / )B + C
BC

2
A .
:
)A - B / (K * F - X ^ M

2 1

3
4
5 :
, .
: X M .
: K F .
: .
: B .
: A .

Arithmetic Statement

17
MATLAB

MATLAB 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

18
MATLAB

MATLAB ,
,:

Sqrt
abs
10 exp
log
log 10
2 log 2
sin
Cos
tan
atan
fix
floor
ceil
round
mod
rem
, , sign
imag
real
factor
true isprime
primes
gcd

19
MATLAB

lcm

:
;>> x = 2.6
;)>> y1 = fix (x); y2 = floor (x); y3 = ceil (x); y4 = round (x
y1 = 2
y2 = 2
y3 = 3
y4 = 3 /
:
.
)sin (a + b) m / sqrt (d

1 3
2
4
5 :
: a .b
: (.)1
: .d
: m (.)3
: ( )4 ( )2 (
).
: :MATLAB

) b = sqrt ( a ^ 2 + 10 b a 2 10

20
MATLAB

)z = log (c * x + n * y )z = ln (cx + ny

y = (sin (x + n * k)) ^ 3 )y = sin 3 (x + nk

)s = atan (y / x )s = tan-1 (y / x

)) r = 2 * sqrt (exp (x 5 r 2 e x 5

x y
)t = abs (x sqrt (y)) / (a + m t
)(a m

3
gp 2
5 ab
)g = p ^ (3 / 2) + (a * b / c) ^ (1 / 5 c


.
. ,
, ,
. , MATLAB
.

MATLAB ,
.
]x = [1, 3, 7, 9, 20 (:)1

0 x )y = sin (x (:)2
x 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
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

21
MATLAB

)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
0 0.3142 0.6283 0.9425 1.2566
,x 1: 5 1
.5
:
)>> x (7: end
= ans
1.885 2.1991 2.5133 2.8274 3.1416
, end
.
:
)>> y (3: -1: 1

22
MATLAB

= ans
0.5878 0.3090 0
, 3: -1: 1 3
1 .1
:
)>> x (2: 2: 7
= ans
0.3142 0.9425 1.5708
,x 2: 2: 7 2
2 .7
:
)]>> y ([8 2 9 1
= ans
0.8090 0.3090 0.5878 0
] [8 2 9 1 y ,
,
. ] [8 2 9 1 .y
:
)]>> y ([1 1 3 4 2 2
= ans
0 0 0.5878 0.8090 0.3090 0.3090
:
MATLAB .
)>> y (3.2
Error
)>> y (3.7
Error

23
MATLAB

)>> y (11.6
Error

x ,
x , 111
,x .
:
1) >> x = (0: 0.1: 1) * pi
) 2) >> x = linspace (0, pi, 11


:
]>> a = [1: 7
=a
1 2 3 4 5 6 7
:
])>> b = [linspace (1, 7, 5
=b
1 2.5 4 5.5 7
:
)>> a = (1: 7
=a
1 2 3 4 5 6 7
:
>> a = 1: 5 , b = 1: 2: 9
=a
1 2 3 4 5
24
MATLAB

=b
1 3 5 7 9
:
,
:
]>> c = [b a
=c
1 3 5 7 9 1 2 3 4 5
c b .a


,
, .
:
]>> c = [1 2 3 4 5
=c
1 2 3 4 5
:
]>> c = [1; 2; 3; 4; 5
=c
1 ( )
2
3
4
5
:
>> a = 1: 5
25
MATLAB

=a
1 2 3 4 5
:
'>> b = a
=b () a .b
1
2
3
4
5
:
;>> k = b ( )
:
>> g = [1 2 3 4 ; 5 ]6 7 8


=g
1 2 3 4
5 6 7 8
:
>> g = [1 2 3 4
5 6 7 8
]9 10 11 12
Enter Return MATLAB
.
:
]>> h = [1 2 3 ; 4 5 6 7

26
MATLAB

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
= ans
2
:
MATLAB ,

( ).
)>> c (12
= ans
4


.
:
>> g 2 g %
= ans
-1 0 1 2
27
MATLAB

3 4 5 6
7 8 9 10

g .2
:
>> 2 * g 1
= ans
1 3 5 7 g
9 11 13 15 ,2

17 19 21 23 .1
:
>> 2 * g / 5 + 1
= ans
1.4 1.8 2.2 2.6
3 3.4 3.8 4.2
4.6 5 5.4 5.8
g ,2
5 ,
.


. ,
,
MATLAB :
>> g %
=g
1 2 3 4
5 6 7 8
9 10 11 12
28
MATLAB

]>> 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
2 3 4 5
7 8 9 10
12 13 14 15
>> ans h
= ans
1 2 3 4
5 6 7 8
9 10 11 12
>> 2 * g h
= ans
1 3 5 7
8 10 12 14
15 17 19 21
)>> 2 * (g h
= ans
0 2 4 6
6 8 10 12
12 14 16 18

, .

29
MATLAB


:
>> g .* h
= ans
1 2 3 4
10 12 14 16
27 30 33 36
g h
.
: MATLAB
, .
>> g * h
Error
h g

:
>> g ./ h
= ans
1.0000 2.0000 3.0000 4.0000
2.5000 3.0000 3.5000 4.0000
3.0000 3.3333 3.6667 4.0000
MATLAB : )( .
. , .
>> g .^ 2
= ans
1 4 9 16
25 36 49 64

30
MATLAB

81 100 121 144


.g

MATLAB ,
, ,
.
)>> ones (3 ( )
= ans
1 1 1
1 1 1
1 1 1
)>> zeros (2, 4 ( )


= ans
0 0 0 0
0 0 0 0
)>> size (g ( )
= ans
3 4
))>> ones (size (g
= ans
1 1 1 1
1 1 1 1
1 1 1 1
: ) ones (n ) zeros (n
MATLAB n n .
)>> eye (4 ( )
31
MATLAB

ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
>> rand (3) )(

ans =
0.9501 0.4860 0.4565
0.2311 0.8913 0.0185
0.6068 0.7621 0.8214
>> rand (1, 5)
ans =
0.4447 0.6154 0.7919 0.9218 0.7382
:
>> 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
>> d + zeros (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
>> repmat (d, 3, 4) (3 4 d (

32
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
1 2 3
4 5 6
7 8 9
>> A (3, 3) = 0
=A
1 2 3 ) (3, 3.
4 5 6
7 8 0
>> A (2, 6) = 1
=A
1 2 3 0 0 0 ) (2, 6 1 A

, 33
.
MATLAB

4 5 6 0 0 1
7 8 0 0 0 0
>> A (:, 4) = 4
=A
1 2 3 4 0 0
4
4 5 6 4 0 1
7 8 0 4 0 0
]>> A (:, 4) = [4; 4; 4
=A
1 2 3 4 0 0
4
4 5 6 4 0 1
7 8 0 4 0 0
]>> A (:, 4) = [4 4 4
Error
:
]>> B = [7 8 9; 4 5 6; 1 2 3
=B
7 8 9
4 5 6
1 2 3
]>> A = [1 2 3; 4 5 6; 7 8 9
=A
1 2 3
4 5 6
7 8 9
])]>> C = [A B(:, [1 3
=C

34
MATLAB

1 2 3 7 9 C A
4 5 6 4 6 .B
7 8 9 1 3
)>> B = A (1: 2, 2: 3
=B
2 3
5 6
: 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

35
MATLAB

3*3 1*9
:
>> A = B
=A
1 2 3
4 5 6
7 8 9
] [ = )>> B (:, 2
=B B
1 3 B , ] [,
4 6
7 9 .
.reshape : ()
'>> C = B
=C
1 4 7
3 6 9
)>> reshape (B, 2, 3
= ans
1 7 6
4 3 9
: reshape
).(transport
: .C
] [ = )>> C (2, :
=C
1 4 7

36
MATLAB

: A .C
>> A (2, :) = C
=A
1 2 3
1 4 7
7 8 9
:
>> x = -3: 3
=x
-3 -2 -1 0 1 2 3
.
,
) (1 true ) (0 false
. :
>> abs (x) > 1
= ans
1 1 0 0 0 1 1
)>> y = x (abs (x) > 1
y x .
=y
-3 -2 2 3
, :
]>> B = [5 -3; 2 -4
=B
5 -3
2 -4
>> x = abs (B) > 2

37
MATLAB

=x

1 1
0 1
)>> y = B (x
=y
5
-3
-4

,
sort ,MATLAB :
)>> x = randperm (8 ( )
=x
7 5 2 1 3 6 4 8
)>> [y, indx] = sort (x

=y
1 2 3 4 5 6 7 8
= indx
4 3 5 7 2 6 1 8
:
( )
])>> A = [randperm (6); randperm (6); randperm (6); randperm (6

38
MATLAB

=A
1 2 5 6 4 3
4 2 6 5 3 3
2 3 6 1 4 5
3 5 1 2 4 6
)>> [As, idx] = sort (A
= As
1 2 1 1 3 1
2 2 5 2 4 3
3 3 6 5 4 5
4 5 6 6 4 6
= idx
1 1 4 3 2 2
3 2 1 4 1 1
4 3 2 2 3 3
2 4 3 1 4 4

,
. MATLAB ,find
,true :
>> x = -3: 3
=x
-3 -2 -1 0 1 2 3
)>> k = find (abs (x) > 1
=k ()
1 2 6 7
39
MATLAB

)>> y = x (k
=y
-3 -2 2 3
)>> y = x (abs (x) > 1
=y
-3 -2 2 3
find ( ) ,:
]>> A = [1 2 3; 4 5 6; 7 8 9
=A
1 2 3
4 5 6
7 8 9
)>> [i, j] = find (A > 6
=i
3
3
3
=j
1
2
3
: diag .
7 8 9 9
7 8 9 9
A=
4 5 8 6

7 8 9 9

)>> diag (A

40
MATLAB

= ans
7
8
8
9
:
MATLAB min , max
.
:
)>> v = rand (1, 6
=v
0.3046 0.1897 0.1934 0.6822 0.3028 0.5417
)>> max (v
= ans
0.6822
)>> [mx, i] = max (v
= mx
0.6822
=i
4
)>> min (v
= ans
0.1897
)>> [mn, j] = min (v
= mn
0.1897
=j

41
MATLAB

2
:
)>> A = rand (4, 6
=A
0.1509 0.8537 0.8216 0.3420 0.7271 0.3704
0.6979 0.5936 0.6449 0.2897 0.3093 0.7027
0.3784 0.4966 0.8180 0.3412 0.8385 0.5466
0.8600 0.8998 0.6602 0.5341 0.5681 0.4449
)>> [mx, r] = max (A
= mx
0.8600 0.8998 0.8216 0.5341 0.8385 0.7027
=r
4 4 1 4 3 2
:
;)'>> max (A ( )
)>> [mn, r] = min (A
= mn
0.1509 0.4966 0.6449 0.2897 0.3093 0.3704
=r
1 3 2 2 2 1
:
;)'>> min (A ( )
>> : .
)mmx = max (mx
= mmx
0.8998
))>> [mmx, i] = max (A (:

42
MATLAB

= mmx
0.8998
=i
8
: :
;))>> z = max (max (A
;))>> z = min (min (A
: .sum
;))>> z = sum (sum (A

,MATLAB
, , :
]>> A = [1 2 3; 4 5 6; 7 8 9
=A
1 2 3
4 5 6
7 8 9
)>> flipud (A up-down
= ans
7 8 9
4 5 6
1 2 3
)>> fliplr (A left-right
= ans
3 2 1
6 5 4
9 8 7
43
MATLAB

)>> triu (A )(upper


= ans
1 2 3
0 5 6
0 0 9
)>> tril (A )(lower
= ans
1 0 0
4 5 0
7 8 9
;)>> g = det (A ()
;)>> h = inv (A ()
;)>> i = eig (A
)>> j = eye (3
=j
1 0 0
0 1 0
0 0 1
;)>> trace (A


, MATLAB length
size numel :
]>> A = [1 2 3 4; 5 6 7 8
=A
1 2 3 4
5 6 7 8
44
MATLAB

)>> S = size (A
=S
2 4
) (2 ).(4
)>> [r, c] = size (A
=r
2
=c
4
)>> r = size (A, 1
=r
2
)>> c = size (A, 2
=c
4
numel :
)>> numel (A
= ans
8
length , :
)>> length (A
= ans
4
>> B = -3: 3
=B
-3 -2 -1 0 1 2 3
)>> length (B

45
MATLAB

= ans
7
))>> min (size (A A
= ans
2
: .
;]>> x = [1 2; 3 4
;]>> y = [x x .^ 2; x .^ 3 x .^ 4

.
MATLAB ( (n-D arrays
. ,
) , (pages ,
,
.
,
.

, :
)>> A = zeros (4, 3, 2
= )A (:, :, 1
0 0 0
0 0 0
0 0 0
0 0 0
= )A (:, :, 2

46
MATLAB

0 0 0
0 0 0
0 0 0
0 0 0
,
.
:
;)>> B (:, :, 1) = zeros (2, 3
;)>> B (:, :, 2) = ones (2, 3
;>> B (:, :, 3) = 4
>> B
= )B (:, :, 1
0 0 0
0 0 0
= )B (:, :, 2
1 1 1
1 1 1
= )B (:, :, 3
4 4 4
4 4 4
reshape
:
])>> C = [B (:, :, 1), B (:, :, 2), B (:, :, 3
=C
0 0 0 1 1 1 4 4 4
0 0 0 1 1 1 4 4 4
)>> reshape (C, 2, 3, 3

47
MATLAB

= )ans (:, :, 1
0 0 0
0 0 0
= )ans (:, :, 2
1 1 1
1 1 1
= )ans (:, :, 3
4 4 4
4 4 4

size .
)>> [r, c, p] = size (C
=r
2
=c
3
=p
3
, ndims
:
)>> ndims (C
= ans
3
)>> numel (C
= ans
18
))>> length (size (C
48
MATLAB

= ans
3

Cell Arrays
MATLAB ,

. ,
( ( ))
,
.
,cell
.
.
MATLAB , } { ,
, :
;}]>> A (1, 1) = {[1 2 3; 4 5 6; 7 8 9
;}>> A (1, 2) = {2 + 3i
;}'>> A (2, 1) = {'Ali Ahmed
;}>> A (2, 2) = {12: -2: 0
>> A
=A
][3 3 double ][2.0000 + 3.0000i

49
MATLAB

''Ali Ahmed ][1 7 double


MATLAB 2 2
,
, .

) ,(cell indexing .

:
A {i, j} = x MATLAB }A (i, j) = {x
x ) (i, j .A
) A (i, j ) , (cell indexing } A {i, j
) (content addressing } { ,
( ) .
:
}>> y = {1, 'hello', 1 > 5
=y
][1] 'hello' [0
}>> y {1
= ans
1
}>> y {2
= ans
hello
}>> y {3
= ans
0
:

50
MATLAB

>> ce = {[1 2 3; 5 6 7], 'yes', 3 > 2};


>> 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]
:
>> x {1} = rand (3, 3);
>> x {2} = rand (3, 3);
>> x {3} = rand (3, 3);
.
.
.
.
.
>> x {9} = rand (3, 3);
>> x {1}
ans =
0.8462 0.6721 0.6813

51
MATLAB

0.5252 0.8381 0.3795


0.2026 0.0196 0.8318
() (>> 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
1 2 3
4 5 6
7 8 9
= )A (2, 1
Ali Ahmed
= )A (1, 2
2.0000 + 3.0000i
= )A (2, 2
12 10 8 6 4 2 0
,

52
MATLAB

,:
}>> A {2, 2
= ans
12 10 8 6 4 2 0
)>> A (2, 2
= ans
][1 7 double
)>> A (1, :
= ans
][3 3 double ][2.0000 + 3.0000i
,ans
.
,
, . :
>> B = {[1 }2], 'John Smith'; 2 + 3i, 5
=B
][1 2 double ''John Smith
][2.0000 + 3.0000i ][5

, , cell
] [ :
)>> C = cell (2, 3
=C
][ ][ ][
][ ][ ][
,
:

53
MATLAB

'>> C (1, 1) = 'The does n't work


Error
,
, .
}'>> C (1, 1) = {'The does n't work
=C
] [ ] [ ''The does n't work
][ ][ ][
}'>> C (2, 3) = {'This works too
=C
''This does work ][ ][
][ ][ ''This works too
, MATLAB
. ,
.

,
, :
>> A
=A
][3 3 double ][2.0000 + 3.0000i
''Ali Ahmed ][1 7 double

>> B
=B
][1 2 double ''John Smith
][2.0000 + 3.0000i ][5
54
MATLAB

]>> C = [A; B ( )
=C
][3 3 double ][2.0000 + 3.0000i
''Ali Ahmed ][1 7 double
][1 2 double ''John Smith
][2.0000 + 3.0000i ][5

:
)>> D = C ([1 3], :
=D
][3 3 double ][2.0000 + 3.0000i
][1 2 double ''John Smith
.
] [ = )>> C (3, :
=C
][3 3 double ][2.0000 + 3.0000i
''Ali Ahmed ][1 7 double
][2.0000 + 3.0000i ][5
reshape , ,
:
;)>> x = cells (3, 4
)>> size (x
= ans
3 4
;)>> y = reshape (x, 6, 2
)>> size (y
= ans

55
MATLAB

6 2
reshape , size
.
repmat
.
:
>> y
=y
][ ][
][ ][
][ ][
][ ][
][ ][
][ ][
)>> z = repmat (y, 1, 3
=z
][ ][ ][ ][ ][ ][
][ ][ ][ ][ ][ ][
][ ][ ][ ][ ][ ][
][ ][ ][ ][ ][ ][
][ ][ ][ ][ ][ ][
][ ][ ][ ][ ][ ][

56
MATLAB


MATLAB ,
.

MATLAB ASCII
,:
'?>> t = 'How about this character string
=t
?How about this character string
)>> size (t
= ans
1 32
>> whos
Name Size Bytes Class
ans 1 2 16 double array
t 1 32 64 character array
Grand total is 34 elements using 80 bytes
.
, , 8
. ASCII

57
MATLAB

,double
:
)>> u = double (t
=u
Columns 1 through 12
72 111 119 32 97 98 111 117 116 32 116 104
Columns 13 through 24
105 115 32 99 104 97 114 97 99 116 101 114
Columns 25 through 32
32 115 116 114 105 110 103 63
)>> char (u
= ans
?How about this character string
))>> char (u (1
= ans
H
,
,MATLAB:
)>> u = t (16: 24
=u
character
, 16 24
character
)>> u = t (24: -1: 16
=u
retcarahc
character .

58
MATLAB

')>> u = t (16: 24
=u
c
h

a
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

59
MATLAB

!'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
Bird
Magic
Jordan
)>> size (legends
= ans
6 6


. MATLAB num2str int2str fprintf
, :
)>> int2str (35
= ans
35
)>> class (ans
= ans
60
MATLAB

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 =
1 1
:
>> radius = sqrt (2);
>> area = pi * radius ^ 2;
>> fprintf ('A circle of radius% 6.4f has an area of % 6.4f', radius, area)
A circle of radius 1.4142 has an area of 6.2832
.area radius % 6.4f
:) (
>> S = ['A circle of radius ', (num2str (radius)), 'has an of ', (num2str (area)) '.']
S=
A circle of radius 1.4121 has an area of 6.2832.

>> S = str2num ('3.5')
S=

3.5
>> t = ['3.5' 'sqrt(2)' ;'1.5' '9.5']
61
MATLAB


=t
)3.5 sqrt(2

1.5 9.5
)>> str2num (t
= ans
3.5000 1.4142

1.5000 9.5000
']>> t = '[3.5sqrt(2);1.59.5
=t
][3.5sqrt(2);1.59.5
)>> str2num (t
= ans
3.5000 1.4142
1.5000 9.5000
findstr .
;'>> b = 'Peter Piper picked a peck of pickled peppers
)'' >> findstr (b,
= ans
6 12 1 9 21 26 29 37
)'>> findstr (b, 'p
= ans

9 13 22 30 38 40 41
)'>> findstr (b, 'cow
= ans
][

62
MATLAB

,
, ,
,
.

, :
}'?>> C = {'How'; 'about'; 'this for a'; 'cell array of strings
=C
''How
''about
''this for a
'?'cell array of strings
)>> size (C
= ans
4 1
} { ,
, C ,
.
, :
)>> C (2: 3
= ans
''about
''this for a
)]>> C ([4 3 2 1
= ans
'?'cell array of strings
''this for a

63
MATLAB

''about
''How
)>> C (1
= ans
''How
))>> size (C (1
= ans
1 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

64
MATLAB

cell array of strings?


>> size (S)
ans =
4 22
: cellstr
>> cellstr (S)
ans =
'How'
'about'
'this for a'
'cell array of strings?'

65
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
66
string
MATLAB

-2 ( ):
:
;}'prompt = {'enter x
;}'def = {'20
;'dlgTitle = 'Input for my program
;lineNo = 1 %
;)answer = inputdlg (prompt, dlgTitle, lineNo, def
;)}x = str2num (answer {1 string num %


:
disp -1
(:)1
;>> d = 15
;)>> disp (d
15
(:)2
;'>> a = 'ali
;)>> disp (a
ali
(:)3
67
MATLAB

;>> sum = 9.8


;)])>> disp (['sum = ', num2str (sum
sum = 9.8
(:)4
;)'>> disp ('computer
computer

(:)1
disp (
).
(:)2
disp (
] [ ( (.)))3

msgbox -2
)'>> msgbox ('ok', 'result


( )

(fprintf )3
(:)1
;>> y = 1.2
;>> x = 100.5
;)>> fprintf ('variable x is % 6.3f\n', x

68
MATLAB

;)>> 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
:
%c
%d
%e
%f
%i
%o
%s
%x
(:)2

.Command Window

69
MATLAB


MATLAB ,

).(True/False
MATLAB
True ,False
True .False
,
.
( ) Relational Operators :
:

<
=<
>
=>
( =) ==
=~

70
MATLAB

,
,
:
(:)1
;>> a = 1; b = 5
>> x = a > b
=x
0
>> A = 1: 9, B = 9 - A
=A
1 2 3 4 5 6 7 8 9
=B
8 7 6 5 4 3 2 1 0
>> tf = A > 4
= tf
0 0 0 0 1 1 1 1 1
A ,4
,A 4 1 .A > 4
)>> tf = (A == B
= tf
0 0 0 0 0 0 0 0 0
A .B
:
(=) (==) , (==)
, (=)
.

71
MATLAB

( :)1 ( )thr (
.)thr
;]>> inddent = [10 17 22 0 7 3 2
;>> thr = 7
)>> y = (indent > thr
=y
1 1 1 0 0 0 0
( :)2 ( )thr (
.)thr
)>> z = inddent .* (inddent > thr
=z
10 17 22 0 0 0 0
( ) Logical Operators:
,
:MATLAB

( and) &
( or) |
( not) ~
:
;>> a = 1
;>> b = 5
>> x = a ~= b
=x
1
)>> b = (1 == 1) & (2 ~= 3

72
MATLAB

=b
1
)>> b = (1==1) | (2 ~= 3
=b
1
))>> b = (1==1) & not ((2 ~= 3
=b
0

;>> A = 1: 9; B = 9 - A
>> tf = A > 4
= tf
0 0 0 0 1 1 1 1 1
A 4
)>> tf = ~ (A > 4
= tf
1 1 1 1 0 0 0 0 0
, .
)>> tf = (A > 2) & (A < 6
= tf
0 0 1 1 1 0 0 0 0
A 2 .6

MATLAB
, ,

73
MATLAB

.
.MATLAB


( )
(') , (^).^ ,
(~)
( * ,).* , ()./ ,/
( ) , ()-
():
(<) , (=<) , (>),
(=>) , (==), (=~)
(&) and
(|) or

if-else-end
.
MATLAB if-else-end :
if expression
)(commands
end

74
MATLAB

end if ) commands(
: .true ( expression)
>> x = 10;
>> if x == 10
disp ('ok')
end;
: if-else-end ,

if expression
(commands evaluated if True)
else
(commands evaluated if False)
end
,true expression
.false expression
: 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)
.
.
.

75
MATLAB

else
(commands evaluated if no other expression is true)
end
:
:)1(
>> x = 10;
>> if x == 10
msgbox ('ok', 'result');
:)2(
>> 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;

3
switch-case

76
MATLAB

,
switch-case :
switch expression
case test-expression1
)(commands1
case test-expression2
)(commands2
otherwise
)(commands3
end
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

77
MATLAB

disp ('this is impossible');


end;

1..5
:)2(
clc;
clear;
n = 3;
switch n
case {0}
m = n + 3;
case {2}
m = 'ali';
case {3}
m = magic (n);
otherwise
disp ('error');
end;
disp (m);

8 1 6
3 5 7
4 9 2
:)3(
x = 2.7;
units = 'm';
switch units
case {'inch', 'in'}

78
MATLAB

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
disp (['unknown units:' units]);
end;

y = 0.027

79
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

80
MATLAB

0.3090 0.5878 0.8090 0.9511 1.0000 0.9511 0.8090


Columns 8 through 10
0.5878 0.3090 0.0000
:
n 1 10 end ,
n ,n = 1 n = 2 .n = 10
: 10 (.)1..10
)>> array = randperm (10
= array
8 2 10 7 4 3 6 9 5 1
>> for n = array
;)x (n) = sin (n * pi / 10
;end
>> x
=x
Columns 1 through 7
0.3090 0.5878 0.8090 0.9511 1.0000 0.9511 0.8090
Columns 8 through 10
0.5878 0.3090 0.0000
n ( )1 ( )10 .array
:
for , :
>> for n =1: 5
for m = 5: -1: 1
;A (n, m) = n ^ 2 + m ^ 2
;end
;)disp (n

81
MATLAB

;end

1
2
3
4
5

>> A
=A
2 5 10 17 26
5 8 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

82
MATLAB

>> for i = 0: 2: 10
disp (i);
end;
0
2
4
6
8
10
>> for i = 10: -2: 1
disp (i);
end;

10
8
6
4
2
>> for i =1: 10
for j = 1: 10
)(
mult (i, j) = i * j;
end;
end; 1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 3 6 40
. . . . . . . . . .

83
MATLAB

. . . . . . . . . .
10 20 30 40 50 60 70 80 90 100
while
while for
, while :
while expression
)(commands
;end
) (commands while end
expression ) ,(true expression .
(:)1
;>> 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
84
MATLAB

;end
>> num
= num
53

:
for while:
( for )while
;s = 0 ;s = 0
for i = 1: 100 ;x = 1
;s = s + i while x < 100
if s > 250 ;s = s + x
;break if s > 250
;end ;break
;end ;end
;x = x + 5
;end

i = 22 x = 51
s = 253 s = 286
:

85
MATLAB

break
.

MATLAB
,MATLAB
,MATLAB save :
>> save
) (Workspace
matlab.mat . ,
,MATLAB , ,
MAT-files ,
,MATLAB
.
save :
>> save var1 var2 var3
var1 var2 var3 ,matlab.mat
save :
>> save filename var1 var2 var3

86
MATLAB

:
var1, var2, var3
.filename.mat
load save save
:
>> load
matlab.mat
.MATLAB
matlab.mat ,
.
) (MAT-file
:
>> load filename var1, var2, var3
filename.mat var1, var2, var3 .

87
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

88
MATLAB

=a
2 4 6 8
4 6 8 10
)>> unique (a
= ans
2
4
6
8
10
ismember :
>> a = 1: 9
=a
1 2 3 4 5 6 7 8 9
>> b = 2: 2: 9
=b
2 4 6 8
)>> ismember (a, b
= ans
0 1 0 1 0 1 0 1 0
)>> ismember (b, a
= ans
1 1 1 1
union .
)>> union (a, b
= ans
1 2 3 4 5 6 7 8 9

89
MATLAB

intersect .
)>> intersect (a, b
= ans
2 4 6 8
setdiff .
)>> setdiff (a, b
= ans
1 3 5 7 9
:
.


,
.
)>> bitand (3, 4
= ans
0
)>> bitor (3, 4
= ans
7
)>> bitxor (13, 27
= ans
22
)>> bitcmp (20, 5 20
= ans
11
)>> bitset (30, 1 30 1
90
MATLAB

= ans

31
)>> bitget (30, 1 30
= ans

0
)>> bitshift (3, 2 ()
= ans
12
)>> bitshift (12, -2 ()
= ans
3
;]>> z = [7 5 4 ; 3 8 9
)>> circshift (z, 1
= ans
3 8 9
7 5 4

MATLAB

dec2bin ,bin2dec :
)>> a = dec2bin (17
= ans
10001
)>> class (a
= ans
char
)>> bin2dec (a
91
MATLAB

= ans
17
)>> class (ans
= ans

double
)>> dec2bin (17, 6
= ans
01001
( )16
hex2dec dec2hex :
)>> a = dec2hex (2047

=a
7FF

)>> dec2hex (2047, 4


= ans
07FF
)>> class (a
= ans
char
)>> hex2dec (a
= ans

2047
)>> class (ans
= ans
double

92
MATLAB


.MATLAB :

;)[out1, out2,, outn] = function_name (input1, input2,...,inputn


:
(:)1
;]>> x = [1, 2, 3, 4, 5, 6, 7, 8
;]>> y = [11, 12, 13, 2, 9, 70
;)>> avgx = average1 (x
;)>> avgy = average1 (y

;)function result = average1 (z


;)L = length (z
()
;)sum1 = sum (z
93
MATLAB

result = sum1 / L;

>> avgx
avgx =
4.5000
>> avgy
avgy =
19.5000


:)2(
>> res1 = mult2 (x);

>> res2 = mult2 (y);

function result = mult2 (x);


) (
result = 2 * x;

>> res1
res1 =
2 4 6 8 10 12 14 16
>> res2
res2 =
22 24 26 4 18 140
:)3(
>> [sin_x, cos_x, x_2] = multf (x);

>> [sin_y, cos_y, y_2] = multf (y);

94
MATLAB

;)function [x1, x2, x3] = multf (x


;)x1 = sin (x
()
;)x2 = cos (x
;x3 = 2 * x

)>> plot (sin_x

:
-1
.
-2 (=)
.
:
;)function y = myfunction (a, b
-------

95
MATLAB

-------
-------
myfunction.m
-3 ).(end
-4 .
-5 .
-6 .


MATLAB
,
, .
plot
.
(:)1
;]x = [1: 0.5: 10
;)y = exp (x ( y)
)plot (x, y x, y
(:)2
;x = 1: 10
)plot (x
:
( ) plot
( )x, x .

96
MATLAB

:)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;
x = 0: pi / 100: 2 * pi;
y = sin (x);
plot (x, y);
legend ('sin (x)');
xlabel ('x = 0: 2: pi'); x
ylabel ('sin (x) cos (x)'); y
title ('plot sin cos function'); ) (

97
MATLAB

(:)6
;))plot (x, sin (x), x, cos (x

(:)7
;)'* plot (x, sin (x), 'r: +', x, cos (x), 'b:
) cos (x )sin (x

98
MATLAB

:
:
;)'text (x, y, 'string



plot3
plot ,plot3 plot
. :
;)plot3 (x1, y1, z1, s1, x2, y2, z2, s2,


( )
:
;)t = linspace (0, 10 * pi, 100
;)plot3 (sin (t), cos (t), t
;)')xlabel ('sin (t
;)')ylabel ('cos (t
;)'zlabel ('t
99
MATLAB

;)'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
;)a = 2 * sin (x) .* cos (x
;)b = sin (x) ./ (cos (x) + eps
;)subplot (2, 2, 1
plot (x, y); axis ([0 2 * pi -1 ;)'1]); title ('Figure1
;)subplot (2, 2, 2
plot (x, z); axis ([0 2 * pi -1 ;)'1]); title ('Figure2
;)subplot (2, 2, 3
plot (x, a); axis ([0 2 * pi -1 ;)'1]); title ('Figure3
100
MATLAB

;)subplot (2, 2, 4
plot (x, b); axis ([0 2 * pi -20 ;)'20]); title ('Figure4


,
surf :
(:)1
;)[x y z] = peaks (30
;)surf (x, y, z
;)'xlabel ('x-axis
;)'ylabel ('y-axis
;)'zlabel ('z-axis

101
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

hist
histogram
:

102
MATLAB

x = -2.9: 0.1: 2.9;


y = randn (10000, 1);
hist (y, x);

pie
pie chart
:
x = [1 3 0.5 2.5 2];
explode = [0 1 0 0 0];
pie (x, explode);

. :
clear;
clc;
corr = [0.0012, 0.0208, 0.0633, 0.1391];
103
MATLAB

amount = [1, 2, 3, 4];


subplot (211);
plot (amount, corr, '--rs');
title ('Cipher-image VS Amount of Encrypted Data');
xlabel ('Amount of Encrypted Data');
ylabel ('Cipher-image Correlation');

104

You might also like