You are on page 1of 26

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
>> MATLAB Command Prompt
MATLAB
>> MATLAB
Windows 95/98 Windows
NT/2000/XP MATLAB
MATLAB
MATLAB ;

>> (5*2+3.5)/5;
MATLAB
ans ans

>> ans

ans =
2.7000

2-2

MATLAB

Chapter

MATLAB

>> x = (5*2+3.5)/5

x =
2.7000

31 MATLAB MATLAB 4 19

MATLAB Variable Declaration


double
Comments%MATLAB

>> 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)*...
>> sin(pi/3);

2-3

MATLAB

2-2
MATLAB Scalars
MATLAB VectorsMatrix

% []

>> s = [1 3 5 2];
>> t = 2*s+1

t =
3

11

MATLAB []Row Vector [1 3 5 2 ]


s [3 7 11 5]
t

s = [1 3 5 2] s = [1, 3, 5, 2]
x = 1:n 1 n
n 1n
MATLAB

% t 2

>> t(3) = 2

t =
3

5
% t 10

>> t(6) = 10

t =
3

10

% t []

>> t(4) = []

t =
3

>> s(2)*3 + t(4)

2-4

10

% s t

MATLAB

Chapter

MATLAB

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

% A

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

>> B = A(2,1:3)

3
5
11

4
8
12

% A 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

2-5

MATLAB

5
9

5
11

8
12

6
5
% A

>> A = [A; 4 3 2 1]

A =
1
5
9
4

3
5
11
3

4
8
12
2

5
6
5
1
% []

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

A =
5
9

5
11

8
12

6
5

MATLAB

MATLAB
MATLAB Column-oriented
VectorIndex
Subscript A
A(2, 3) A(6)
A
A(:) A MATLAB
A(:) 81

2-3
MATLAB

>> x = 4;

2-6

MATLAB

Chapter

>> y = abs(x)

MATLAB

% 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

>> z = 5 + 4i

% z = 5 + 4 1

z =
5.0000 + 4.0000i
>> y = angle(z)

% z

y =
0.6747
>> y = real(z)

% z

y =
5
>> y = imag(z)

% z

y =

2-7

MATLAB

4
>> y = conj(z)

% z

y =
5.0000 - 4.0000i
% z

>> y = z

y =
5.0000 - 4.0000i

>> y = exp(j*pi/6)

e j = cos + j sin

y =
0.8660 + 0.5000i
pi MATLAB 3.1415926

Euler Identity
Euler Identity

Euler

>> x = [4 2j 9];
>> y = sqrt(x)

y =
2.0000

1.0000 + 1.0000i

% x
3.0000

sqrt x

>> x = [1 2 3 0 12];
>> y = min(x)

y =

2-8

MATLAB

% x

Chapter

MATLAB

0
% x

>> y = max(x)

y =
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];

% x

>> y = median(x)

y =
4

6
% x

>> y = prod(x)

y =
28

80

162

MATLAB help On-line Help

>> help sort

% sort

2-9

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
help inv help inv
inv help help help

2-10

MATLAB

Chapter

MATLAB

lookfor lookfor inverse


MATLAB inverse help
lookfor M
M
helpwin helpdesk MATLAB

docdoc eig

2-4
MATLAB LoopsConditionsFlow Control
for For-loop
For =
;
end
for end

6 Harmonic Sequence

>> x = zeros(1,6);

% x 16

>> for i = 1:6

x(i) = 1/i;

>>
>> end

% x

>> disp(x)

1.0000

0.5000

0.3333

0.2500

0.2000

0.1667

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

2-11

MATLAB

while
;
end
for
while

% x 16

>> x = zeros(1,6);
>> i = 1;
>> while i <= 6
>>

x(i) = 1/i;

>>

i = i + 1;

>> end

% x

>> disp(x)

1.0000

0.5000

0.3333

0.2500

0.2000

0.1667

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

2-12

MATLAB

Chapter

>>

MATLAB

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

>> end

Given random number is less than 0.5.

2-5 M
MATLAB m
MATLAB MATLAB
m M M-files myTest.m M
MATLAB myTest

>> pwd

% pwd = present working directory

ans =
C:\MATLAB6p5\work
cd myTest.m

>> cd d:\matlabBook\MATLAB\02-MATLAB

d:\matlabBook\MATLAB
http://www.cs.nthu.edu.tw/~jang

myTest.m

>> type myTest.m

% myTest.m

% myTest: my first test M-file.

2-13

MATLAB

% Roger Jang, March 3, 1997


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

% myTest.m

>> myTest

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

H1 Help Line
myTest.m
M lookfor M
myTest.m myTest lookfor myTest
MATLAB myTest M myTest.m

Scripts

Functions

myTest.m MATLAB MATLAB

Input ArgumentsOutput Arguments C


FORTRAN Subroutines
Factorial MATLAB fact01.m

>> type fact01.m

function output = fact01(n)


% FACT01 Calculate factorial of a given positive integer (for-loop
version)
output = 1;

2-14

MATLAB

Chapter

MATLAB

for i = 1:n,
output = output*i;
end
fact01 n output i

>> y = fact01(5)

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

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

>> type fact02.m

function output = fact02(n)


% FACT02 Calculate factorial of a given positive integer (recursive
version)
if n == 1,
% Terminating condition
output = 1;
return;
end
output = n*fact02(n-1);

2-15

MATLAB

MATLAB n
n! prod(1:n) gamma gamma(n-1)
Terminating Condition

n==1 output 1
n

2-6
myTest.m d:\matlabBook\MATLAB\02MATLABMATLAB M

MATLAB

myTest.m

d:\matlabBook\MATLAB\02-MATLAB MATLAB
Search Path

MATLAB
MATLAB myTest
1. myTest
2. myTest
3. myTest M
4. myTest M
5. MATLAB
MATLAB path
>> path

MATLABPATH
C:\MATLAB6p5\toolbox\matlab\general
C:\MATLAB6p5\toolbox\matlab\ops
C:\MATLAB6p5\toolbox\matlab\lang

2-16

MATLAB

Chapter

MATLAB

C:\MATLAB6p5\toolbox\matlab\elmat
C:\MATLAB6p5\toolbox\matlab\elfun

Toolboxes
which

>> which demo

C:\MATLAB6p5\toolbox\matlab\demos\demo.m
>> cd(matlabroot);

% MATLAB

>> which fact01

fact01 not found.


d:\matlabBook\MATLAB\02-MATLAB MATLAB
MATLAB fact01.m M
d:\matlabBook\MATLAB\02-MATLAB MATLAB
addpath

>> addpath('d:\matlabBook\MATLAB\02-MATLAB');

d:\matlabBook\MATLAB \02- MATLAB


MATLAB path MATLAB
fact01.m

>> which fact01

d:\matlabBook\MATLAB\02-MATLAB\fact01.m
fact01 10! fact01(10)
fact01.m

MATLAB

2-17

MATLAB

MATLAB
MATLAB
1.

MATLAB matlabrc.m MATLAB


MATLAB
matlabrc.m

2.

MATLAB matlabrc.m startup.m


MATLAB

MATLAB command(string)command string


addpath('d:\matlabBook')addpath d:\matlabBookaddpath

addpath d:\matlabBook -end rmpath
rmpath d:\matlabBook
MATLAB pathtool

pwd pwd = present working directory


cd
dir
OS MATLAB OS
!dir DOS dir MATLAB

2-7
MATLAB
Base WorkspaceWorkspace MATLAB

MATLAB

2-18

MATLAB

Chapter

MATLAB

MATLAB

Workspace who

>> who

Your variables are:


A
B

ans
i

s
t

x
y

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

MATLAB workspace
clear

>> clear A

% A

2-19

MATLAB

>> clear all

MATLAB Permanent Constants


>> pi

ans =
3.1416

MATLAB

i j

eps

Floating-point

inf

1/0

nan NaN

Not A Number0/0

pi

= 3.1415926...

realmax

realmin

nargin

nargout

MATLAB

save load

Optionssave Binary
mat
save matlab.mat
save filename filename.mat
save filename x y z xyz filename.mat

2-20

MATLAB

Chapter

MATLAB

>> cd 'd:\matlabBook\MATLAB\02-MATLAB'

save

>> clear all

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

% x, y
%

>> whos

Name
x
y

Size
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
>> delete test.mat

% test.mat

MS Word
ASCII
save filename x -ascii x 8 bytes
filename ASCII
save filename x -ascii -double x

16 bytes

filename ASCII
save filename -ascii -tabs8 bytes
filename ASCII Tab

ASCII
save ascii

2-21

MATLAB

1. save mat mat


MATLAB
2. save
load load
3. load
4.
5. ASCII

load

load filenameload filename.mat


filename.mat filename ASCII
load filename -asciiload filename ASCII

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

2-22

MATLAB

Chapter

MATLAB

MATLAB Load Wizard


MATLAB File/Import Data...
MATLAB

2-8 MATLAB
MATLAB

1.

exit

2.

quit

3.

MATLAB

2-9
1.

MATLAB
(a) i
(b) j
(c) eps
(d) inf
(e) nan
(f) pi
(g) realmax
(h) realmin

2.

lookfor MATLAB
MATLAB
(a)
(b) 46 122
(c) Left-right flip
(d) Up-down flip

2-23

MATLAB

(e)
(f)
(g) Rotate
(h) Inverse matrix
(i) rank
(j) reduced row echelon form
(k) null space
(l) EigenvaluesEigenvectors
(m) QR QR Decomposition
(n) LU LU Decomposition
(o) Singular Value Decomposition
(p) Fast Fourier Transform
(q)
(r)
3.

MATLAB findN01.m n n! > realmax n


(n-1)!

4.

MATLAB sqrt

(a)
(b) 2*i
(c) -5+12*i
i

5.

MATLAB myFun01.m
y = 0.5*exp(x/3)-x*x*sin(x)
x y x

x=0:0.1:10;
plot(x, myFun01(x));

6.

(a) MATLAB piFun01.m


f(n) = 4*(1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ...)

2-24

MATLAB

Chapter

MATLAB

n f(n)
(b) tic toc piFun01(100000)
help tic help toc Pentium
450MHz 2
7.

(a) MATLAB fibo.m Fibonacci


fibo(n+2) = fibo(n+1)+fibo(n)

fibo(1) = 0, fibo(2) = 1.
(b) fibo(25)
(c) tic toc fibo(25)
help tic help toc Pentium 2GHz
3.35
(d) Fibonacci n
fibo(n)={[(1+a)/2]^(n-1)-[(1-a)/2]^(n-1)}/a
a 5 MATLAB fibo2.m
Fibonacci
(e) fibo2(25) fibo(25)
(f) fibo2(25) fibo(25)
(g) fibo.m fibo2.m Fibonacci

8.

(a) minxy.m
[minValue, minIndex] = minxy(matrix)
matrix minValue minIndex
2 matrix(minIndex(1),
minIndex(2)) minValue
(b) [minValue, minIndex] = minxy(magic(20)) minValue
minIndex
min

2-25

MATLAB

9.

a = [92, 95, 58, 75, 69, 82] sort


[b, index] = sort(a)
b = [58, 69, 75, 82, 92, 95] index = [3, 5, 4, 6, 1, 2] index
b a b a(index) sort01.m
a index2 a b(index2) a index2
[5, 6, 1, 3, 2, 4]
for-loop while-loop
sort

10.

x
ranking01.m x x = [92, 95, 58,
75, 69, 82] ranking(x) [2, 1, 6, 4, 5, 3] 92
195 258 6
for-loop while-loop
sort

2-26

MATLAB

You might also like