Introduction To Matlab: Doikon@
Introduction To Matlab: Doikon@
[email protected]
http://www.telecom.tuc.gr
Workspace
view program variables clear to clear double click on a variable to see it in the Array Editor
Command History
view past commands save a whole session using diary
Launch Pad
access tools, demos and documentation
Matrices
a vector
x = 1 2 5 1 x = [1 2 5 1]
a matrix
x = 1 5 3 2 1 2
x = [1 2 3; 5 1 4; 3 2 -1]
3 4 -1 y = x. y = 1 2 5 1
transpose
Matrices
x(i,j) subscription
y=x(2,3)
y = 4
y=x(3,:)
whole row
y = 3 2 -1
whole column
y=x(:,2) y = 2 1 2
Operators (arithmetic)
+ * / ^ addition subtraction multiplication division power complex conjugate transpose
.* ./ .^ .
equal not equal less than less than or equal greater than greater than or equal AND OR NOT
ones(M,N)
rand(M,N)
0.6068
Operators
x = [ zeros(1,3) ones(1,2) ] x = 0 0 0 1 1
x = [ 1 3 5 7 9] x = 1 3 5 7 9 y = x(2) y = 3 y = x(2:4) y = 3 5 7
[ ] concatenation
( ) subscription
Matlab Graphics
x = 0:pi/100:2*pi; y = sin(x); plot(x,y) xlabel('x = 0:2\pi') ylabel('Sine of x') title('Plot of the Sine Function')
Multiple Graphs
t = 0:pi/100:2*pi; y1=sin(t); y2=sin(t+pi/2); plot(t,y1,t,y2) grid on
Multiple Plots
t = 0:pi/100:2*pi; y1=sin(t); y2=sin(t+pi/2); subplot(2,2,1) plot(t,y1) subplot(2,2,2) plot(t,y2)
linear plot discrete plot add grid lines add X-axis label add Y-axis label add graph title divide figure window create new figure window wait for user response
Math Functions
Elementary functions (sin, cos, sqrt, abs, exp, log10, round)
type help elfun
Functions
function f=myfunction(x,y) f=x+y;
Flow Control
if switch for
statement statement
loops loops statement statement
while
continue
break
Miscellaneous
Loading data from a file
load myfile.dat
Suppressing Output
x = [1 2 5 1];
Getting Help
Using the Help Browser (.html, .pdf)
View getstart.pdf, graphg.pdf, using_ml.pdf
Type
help help function, e.g. help plot
Running demos
type demos type help demos
Random Numbers
x=rand(100,1); stem(x);
hist(x,100)
Coin Tosses
Simulate the outcomes of 100 fair coin tosses
x=rand(100,1); p=sum(x<0.5)/100 p =
0.5400
Coin Tosses
Simulate the outcomes of 1000 biased coin tosses with p[Head]=0.4
x=rand(1000,1); p=sum(x<0.4)/1000 p = 0.4160
6 5 4 3 2 1
(1,6) (2,6) (3,6) (4,6) (5,6) (6,6) (1,5) (2,5) (3,5) (4,5) (5,5) (6,5) (1,4) (2,4) (3,4) (4,4) (5,4) (6,4) (1,3) (2,3) (3,3) (4,3) (5,3) (6,3) (1,2) (2,2) (3,2) (4,2) (5,2) (6,2) (1,1) (2,1) (3,1) (4,1) (5,1) (6,1) 1 2 3 4 5 6
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
p[2]=0.0278 p[3]=0.0556 p[4]=0.0833 p[5]=0.1111 p[6]=0.1389 p[7]=0.1667 p[8]=0.1389 p[9]=0.1111 p[10]=0.0833 p[11]=0.0556 p[12]=0.0278
Bernoulli
1720
n = 20
p = 0.5
n = 20
p = 0.1
Combinatorics
Permutations:
Permutations:
n objects
choose k objects from n
Pkn =
n!
n! (n - k )!
(hint: fill 2 spaces on a bookshelf with books chosen from 5 available books)
Combinations:
n! k !(n - k )!
n = k
n n - k
n = 0
n n! = =1 n 0!n !
n = 1
n n! = = n n - 1 1!(n - 1)!