Title: A Short Introduction to Matlab
1A Short Introduction to Matlab
- Erik Zeitler
- Uppsala Database Laboratory
2Matlab
- MATrix LABoratory
- A high level programming language
- perform numerical computations
- produce graphical output
- Basic data type Matrix
- Very fast matrix manipulation
- Programming control structures are available
- procedures
- loops
- , but these are slow.
Good news You dont need a lot of loops. Use
matrices instead.
3Syntax
- Matlab uses standard infix notation
- gtgt 25
- ans
- 7
- gtgt 9-3
- ans
- 6
- gtgt 1210
- ans
- 120
- gtgt 53
- ans
- 125
4Variable Assignment
- gtgt A214
- gtgt Bsqrt(A)
- gtgt C23i
- C
- 2.0000 3.0000i
- gtgt DABC
- D
- 22.0000 3.0000i
5Matrix creation
gtgt A15 A 1 2 3 4 5 gtgt A1 2 3 4 5 A 1 2 3 4 5
gtgt A127 A 1 3 5 7 gtgt A1 2 3 1 2 3 A 1 2 3 1 2 3
6Matrix creation
gtgt zeros(3,5) ans 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 gtgt B1 2 3 B 1 2 3
gtgt ones(3,4) ans 1 1 1 1 1 1 1 1 1 1 1 1 gtgt ABB A 1 2 3 1 2 3
7Matrix Manipulation
A 1 2 3 1 2 3
gtgt A' ans 1 1 2 2 3 3 gtgt A3 ans 0 0 1 0 0 1 gtgt A(3,) 2 3 4 A 1 2 3 1 2 3 2 3 4
gtgt A(,2) ans 2 2 gtgt rot90(A) ans 3 3 2 2 1 1 gtgt A(,4) 44 A 1 2 3 4 1 2 3 4
8Matrix Arithmetics
A 1 2 3 1 2 3
gtgt AA ans 2 4 6 2 4 6 gtgt AA ??? Error using gt mtimes Inner matrix dimensions must agree. gtgt AA ??? Error using gt mtimes Inner matrix dimensions must agree.
gtgt A.A ans 1 4 9 1 4 9 gtgt AA' ans 14 14 14 14 gtgt A'A ans 2 4 6 4 8 12 6 12 18
9Some useful tricks
A 1 2 3 1 2 3
gtgt repmat(A,1,2) ans 1 2 3 1 2 3 1 2 3 1 2 3 gtgt find(A3) ans 5 6
gtgt randperm(4) ans 2 4 3 1 gtgt size(A) ans 2 3
10Some useful tricks
A 1 2 3 1 2 3
gtgt sum(A) ans 2 4 6 gtgt sum(A,2) ans 6 6 gtgt sum(sum(A)) ans 12 gtgt sort(randperm(4)) ans 1 2 3 4
11More tricks
gtgt B1 2 3 4 5 6 B 1 2 3 4 5 6 gtgt std(B) ans 2.1213 2.1213 2.1213
gtgt mean(B) ans 2.5000 3.5000 4.5000 gtgt var(B) ans 4.5000 4.5000 4.5000
12Exercises (to do in pairs)
- Given a (1N) vector A, create a (3N) matrix B
such that - the first row in B contains A
- the second row in B contains A backwards
- the third row in B contains the sum of A and A
backwards - Given a matrix C, create a sorted vector D which
contains all of the values in C which are greater
than 10
13Other useful functions
- princomp(Data)
- Returns the principal components.
- plot(A, B, Symbol)
- where Symbol is one of '' 'o' '' etc
- hold on/off
- grid on/off
14if
if condition statements elseif condition statements else statements end gtgt A0 1 2 gtgt B1 2 3 gtgt if AgtB CA-B elseif BgtA CB-A else CAB end
15for and while
for i vector statements end example j0 for i110 jij end while condition statements end example j0, i1 while ilt10 jij, ii1 end
16for vs. while
j12 a0 for i1size(j) if j(i)1 jj,i else aaj(i) end end j12 a0 while(iltsize(j)) if j(i)1 jj,i else aaj(i) end end
Interrupt execution Ctrl-C
17Functions in Matlab
- Create a function fun
- function Ret1, RetM fun(Arg1, , ArgN)
- statements defining Ret1, , RetM
- end
- This definition must go in a file called fun.m
- Invoking fun from Matlab
- (make sure your cd to the dir where fun.m is)
- gtgt x1, , xM fun(a1, , aN)
- will set the variables x1, , xM.
18traparea.m
- function Area traparea(A,B,H)
- Calculates the area of a trapezoid
- with length A, B of the parallell
- sides and the distance H between the
- sides.
- Area0.5(AB)H
- end
19cylinder.m
- function Vol, Area cylinder(R,H)
- calculates the volume and surface
- area of a cylinder with radius R and
- height H
- Area2piRH
- Vol(AreaR)/2
- end
20Exercise (to do in pairs)
- Write a function subtract_and_scale
- Given this input
- a matrix of dimension NM,
- a vector of size 1M
- Calculate a new matrix, by
- subtracting the vector from each row in the
matrix, - multiplying each value in the new matrix by 2
- function NewMatrix subtract_and_scale(Matrix,
Vector)
21Think vectors!
- Instead of
- nofRows,nofCols size(A)
- for row 1nofRows
- for col 1nofCols
- A(row, col) A(row, col) 1
- end
- end
- Write
- A A 1
- ? More compact and much more efficient
22Think vectors!
- Example (script run.m)
- Xones(500,500)
- Yones(500,500)
- tic start timer
- for i1500
- for j1500
- Val 0
- for k 1500
- Val X(i,k)Y(k,j) Val
- end
- Z(i,j) Val
- end
- end
- toc stop timer and print elapsed time
- tic start timer
- Z XY
- toc stop timer and print elapsed time
gtgt run Elapsed time is 3.47 seconds Elapsed time
is 0.15 seconds
23Learn more
- Pärt-Enander, Sjöberg, Pihl Användning av Matlab
- English version is available too
- For sale at UTH-Gård
- There is an abundance of Matlab tutorials on the
internet