You are on page 1of 27

MATLAB

MATLAB
MATLAB

MATLAB MATLAB

MATLAB

2-1
MATLAB +-*/
^ MATLAB

MATLAB Command
Window>> Enter

>>

(5*2+3.5)/5

ans =
2.7000
MATLAB ans MATLAB
Answer

2.7000
MATLAB
;

>>

(5*2+3.5)/5;

MATLAB
8 >> MATLAB Command W indow
Command Prompt MATLAB

8 >> MATLAB
W indows 95/98
W indows NT
MATLAB MATLAB

2-2

MATLAB

Chapter

MATLAB

MATLAB
ans
ans

>>

ans

ans =
2.7000
x

>>

x = (5*2+3.5)/5

x =
2.7000
MATLAB %
Comments

>>

y = (5*2+3.5)/5;

% y

>>

z = y^2

% z

z =
7.2900
MATLAB
MATLAB
MATLAB ,;

>>

x = sin(pi/3); y = x^2; z = y*10,

z =
7.5000

>>

z = 10*sin(pi/3)*...

2-3

MATLAB

sin(pi/3);

>>

8
8
8 31 MATLAB MATLAB 4
19

8 MATLAB Variable Declaration


double

2-2
MATLAB Scalars
MATLAB VectorsMatrix

>>

s = [1 3 5 2];

>>

t = 2*s+1

% []

t =
3

11

MATLAB [] [1 3 5 2 ]
14 [1 3 5 2 ] s
[3 7 11 5] t

2-4

MATLAB

Chapter

MATLAB

8 S = [1 3 5 2] S = [1, 3, 5, 2]

MATLAB

>>

% t 2

t(3) = 2

t =
3
>>

5
% t 10

t(6) = 10

t =
3
>>

10

% t

t(4) = []

t =
3
>>

10
% s

s(2)*3 + t(4)

% t
ans =
9
>>

% t

t(2:4) - 1

ans =
6

-1

mn m
n
;

>>

A = [1 2 3 4; 5 6 7 8; 9 10 11 12];

% 34 A

>>

% A

2-5

MATLAB

A =
1
5
9

2
6
10

3
7
11

4
8
12

% A 5

A(2,3) = 5

>>

A =
1
5
9
>>

2
6
10

3
5
11

4
8
12
% A

B = A(2,1:3)

% B
B =
5
>>

5
% B A

A = [A B']

A =
1
5
9
>>

2
6
10

A(:, 2) = []

3
5
11

4
8
12

5
6
5

% A []

A =
1
5
9
>>

3
5
11

4
8
12

5
6
5
% A

A = [A; 4 3 2 1]

A =
1
5

2-6

3
5

4
8

5
6

MATLAB

Chapter

9
4
>>

11
3

12
2

MATLAB

5
1
%

A([1 4], :) = []

% []
A =
5
9

5
11

8
12

6
5

MATLAB
8 MATLAB
Column-orientedVector
IndexSubscript
A
A(2, 3) A(6) A
Column Vector

8 A(:) A MATLAB
A(:) 81

2-7

MATLAB

2-3
MATLAB

>>

x = 4;

>>

y = abs(x)

% x

y =
4
>>

y = sin(x)

% x

y =
-0.7568
>>

y = exp(x)

% exp(x)

y =
54.5982
>>

y = log(x)

% ln(x)

y =
1.3863

MATLAB i j

>>

z = 5 + 4j

% z = 5 + 4 1

z =
5.0000 + 4.0000i
>>

y = angle(z)

y =
0.6747

2-8

MATLAB

% z

Chapter

>>

y = real(z)

MATLAB

% z

y =
5
>>

y = imag(z)

% z

y =
4
>>

y = conj(z)

% z

y =
5.0000 - 4.0000i

>>

y = exp(j*pi/6)

e j = cos + j sin

y =
0.8660 + 0.5000i

>>

x = [4 2j 9];

>>

y = sqrt(x)

y =
2.0000

% x
1.0000 + 1.0000i

3.0000

sqrt x

>>

x = [1 2 3 0 12];

>>

y = min(x)

% x

y =
0
>>

y = max(x)

% x

y =

2-9

MATLAB

12
>>

% x

y = mean(x)

y =
3.6000
>>

% x

y = sum(x)

y =
18
>>

% x

y = sort(x)

y =
0

12

>>

x = [1 2 3;4 5 6;7 8 9];

>>

y = median(x)

% x

y =
4
>>

6
% x

y = prod(x)

y =
28

80

162

MATLAB help
On-line Help

>>

2-10

help sort

MATLAB

% sort

Chapter

MATLAB

SORT

Sort in ascending order.


For vectors, SORT(X) sorts the elements of X in ascending
order.
For matrices, SORT(X) sorts each column of X in ascending
order.
For N-D arrays, SORT(X) sorts the along the first nonsingleton dimension of X. When X is a cell array of
strings, SORT(X) sorts the strings in ASCII dictionary
order.
SORT(X,DIM) sorts along the dimension DIM.

[Y,I] = SORT(X) also returns an index matrix I. If X is


a vector, then Y = X(I). If X is an m-by-n matrix, then
for j = 1:n, Y(:,j) = X(I(:,j),j); end
When X is complex, the elements are sorted by ABS(X).
Complex matches are further sorted by ANGLE(X).
Example: If X = [3 7 5
0 4 2]
then sort(X,1) is [0 4 2 and sort(X,2) is [3 5 7
3 7 5]
0 2 4];
See also SORTROWS, MIN, MAX, MEAN, MEDIAN.
Overloaded methods
help cell/sort.m

MATLAB

2-11

MATLAB

8 help inv help


inv inv help help help

8 lookfor lookfor
inverseMATLAB inverse
help lookfor
M

8 helpwin MATLAB

8 helpdesk HTML pdf


8 doc HTML doc
eig

2-4
MATLAB LoopCondition
Flow Control for
For-loop
for =
;
end

for end
6
Harmonic Sequence

2-12

MATLAB

Chapter

>>

x = zeros(1,6);

>>

for i = 1:6

MATLAB

% x 16

x(i) = 1/i;

>>
>>

end

>>

disp(x)
1.0000

% x
0.5000

0.3333

0.2500

0.2000

0.1667

x 16 for
i 1 6 [1 2 3 4 5 6 ]
x i 1/i
while While-loop

while
;
end

for while

>>
>>
>>

x = zeros(1,6);

% x 16

i = 1;
while i <= 6

>>

x(i) = 1/i;

>>

i = i + 1;

>>

end

>>

disp(x)
1.0000

0.5000

0.3333

0.2500

0.2000

0.1667

2-13

MATLAB

8 zeros Pre-allocate
MATLAB

zeros ones

MATLAB if
else end
if
;
else
;
end
>>

if rand(1,1) > 0.5


disp('Given random number is greater than 0.5.');

>>
>>

else
disp('Given random number is smaller than 0.5.');

>>
>>

end

Given random number is smaller than 0.5.

2-14

MATLAB

Chapter

MATLAB

2-5 M
MATLAB
m MATLAB
MATLAB m M

M-files
test.m M MATLAB
test

>>

pwd

ans =
d:\work
>>cd

d:\mlbook\examples

>>type

test.m

% test.m
% test.m

% This is my first test M-file.


% Roger Jang, March 3, 1997
fprintf('Start of test.m!\n');
for i = 1:3,
fprintf('i = %d ---> i^3 = %d\n', i, i^3);
end
fprintf('End of test.m!\n');
>>

test

% test.m

Start of test.m!
i = 1 ---> i^3 = 1
i = 2 ---> i^3 = 8
i = 3 ---> i^3 = 27
End of test.m!

2-15

MATLAB

H1 Help Line
8 test.m
M lookfor
M test.m test
lookfor testMATLAB test M
test.m

M
Scripts
Functions
test.m MATLAB
MATLAB

Input ArgumentsOutput Arguments

FORTRAN

Subroutines
Factorial
MATLAB fact1.m

>>

type fact1.m

function output = fact(n)


% FACT Calculate factorial of a given positive integer (forloop version)
output = 1;
for i = 1:n,
output = output*i;
end
fact1 n output i

>>

2-16

y = fact1(5)

MATLAB

Chapter

MATLAB

y =
120
fact1 fact.m
fact(5) MATLAB Temperary
Workspace n 5
n i
output
MATLAB output y

MATLAB Recursive
n! = n*(n-1)!

>>

type fact2.m

function output = fact2(n)


% FACT Calculate factorial
(recursive version)

of

given

positive

integer

if n == 1, % Terminating condition
output = 1;
return;
end
output = n*fact(n-1);

2-17

MATLAB

8 MATLAB
n n! prod(1:n)
gamma gamma(n-1)

Terminating Condition
n==1
output 1
M M

2-6
test.m d:\mlbook\examples
MATLAB M
MATLAB

test.m

d:\mlbook\examples MATLAB Search Path

MATLAB
8 MATLAB test
1. test
2. test
3. test M
4. test M
5. MATLAB

2-18

MATLAB

Chapter

MATLAB

MATLAB path

>>

path
MATLABPATH

d:\mlbook\examples
C:\MATLABR11\toolbox\matlab\general
C:\MATLABR11\toolbox\matlab\ops

Toolboxes
which

>>

which demo

C:\MATLABR11\toolbox\matlab\demos\demo.m
>>

cd(matlabroot);

>>

which test

% MATLAB

test not found.


d:\mlbook\examples MATLAB
MATLAB test.m M
d:\mlbook\examples MATLAB
addpath

>>

addpath('d:\mlbook\examples');

d:\mlbook\examples MATLAB
path MATLAB
test.m

>>

which test

d:\mlbook\examples\test.m

2-19

MATLAB

test
test.m

MATLAB
8 MATLAB
MATLAB

1. MATLAB matlabrc.m c:\matlab


MATLAB MATLAB
matlabrc.m
2. MATLAB matlabrc.m
startup.m
MATLAB

8 MATLAB command(string)command string


addpath(d:\mlbook\examples)addpath
d:\mlbook\examples

8 addpath
addpath d:\mlbook\examples -end
rmpath rmpath d:\mlbook\examples

8 MATLB pathtool

2-20

MATLAB

Chapter

MATLAB

pwd pwd = present working directory

cd

dir

OS MATLAB OS
win98 !dir DOS dir MATLAB

2-7
MATLAB
Base Workspace
Workspace MATLAB

MATLAB

MATLAB

Workspace who

>>

who

Your variables are:


A
B

ans
i

s
t

x
y

2-21

MATLAB

whos

>>

whos

Name

Size

Bytes

A
B
ans
i
s
t
x
y
z

2x4
1x3
1x18
1x1
1x4
1x5
1x6
1x1
1x1

64
24
36
8
32
40
48
8
16

Class
double array
double array
char array
double array
double array
double array
double array
double array
double array (complex)

Grand total is 47 elements using 276 bytes

8 MATLAB

8 MATLAB workspace

clear

2-22

>>

clear A

% A

>>

clear all

MATLAB

Chapter

MATLAB

MATLAB Permanent Constants


>>

pi

ans =
3.1416

MATLAB

ij

eps

Floating-point

inf

1/0

nan NaN

Not A Number0/0

pi

= 3.1415926...

realmax

realmin

nargin

nargin

MATLAB

save
load

Optionssave
Binary mat
l save matlab.mat

2-23

MATLAB

l save filename filename.mat

l save filename x y z xyz filename.mat

save

>>cd

>>clear
>>x

d:\mlbook\examples

all

% x, y

= 10; y = [1, 2, 3];

>>whos

Name

Size

x
y

1x1
1x3

Bytes
8
24

Class
double array
double array

Grand total is 4 elements using 32 bytes


>>

save test x y

% x y test.mat

>>

dir *.mat

% MAT

test.mat
>>

tguide1.mat

delete test.mat

tguide2.mat
% test.mat

MS Word

ASCII

l save filename x -ascii x


8 bytes filename ASCII

2-24

MATLAB

Chapter

MATLAB

l save filename x -ascii -double x


16 bytes filename ASCII

l save filename -ascii -tabs


8 bytes filename ASCII
Tab

ASCII
8 save ascii
1. save mat mat
MATLAB
2. save
load load

3. load

4.
5. ASCII

load

l load filenameload filename.mat


filename.mat
filename ASCII
l load filename -asciiload filename
ASCII

2-25

MATLAB

ASCII

>>

clear all;

>>

x = 1:10;

>>

save testfile.dat x -ascii

%
% x ASCII
% testfile.dat

>>

load testfile.dat

% testfile.dat

>>

who

Your variables are:


testfile

ASCII
testfile x

MATLAB
8 MATLAB Load W izard
MATLAB File/Import Data...

MATLAB

2-26

MATLAB

Chapter

MATLAB

2-8 MATLAB
MATLAB

1. exit
2. quit
3. MATLAB

2-27