Introduction to MATLAB - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Introduction to MATLAB

Description:

Writing functions/ scripts as .m files. Passing scripts from the Linux shell. 7. MATLAB Language ... Scripting MATLAB from Linux Shell. 2 options I've used: ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 27
Provided by: noth5
Category:

less

Transcript and Presenter's Notes

Title: Introduction to MATLAB


1
Introduction to MATLAB
  • Ethan Katz-Bassett
  • ethan_at_cs
  • November, 2007

2
What is MATLAB?
  • MATrix LABoratory
  • Numerical computing environment
  • Mathematical functions
  • Visualizations
  • Programming language

3
What is MATLAB good for?
  • Easy matrix manipulation/ data exploration
  • Implementation of numerical algorithms
  • Plotting of functions/data
  • User interfaces??
  • Interfacing with programs in other languages?

4
Some things Ive used it for
  • Plotting graphs
  • Plotting geographic data on maps
  • Writing various numerical algorithms
  • Solving linear programs
  • Defining and solving semidefinite programs on
    lots of variables
  • K-means and other clustering

5
MATLAB in the Department
  • /projects/matlab/bin/matlab
  • Includes many toolboxes (signal processing,
    optimization, statistics, ), but not all
  • Limited number of licenses
  • /projects/matlab/etc/lmstat -A
  • MATLAB compiler (mcc)?
  • Licenses for Mac betas available (at least last
    January). See Mac-users archives
  • Some people have Windows copies
  • Licenses available for purchase-- 100 plus

6
Ways to use it
  • Interactive interpreter in Command Window
  • Writing functions/ scripts as .m files
  • Passing scripts from the Linux shell

7
MATLAB Language
  • diary('ethan/matlab_tutorial/diary.txt')
  • diary on
  • Looks clean, like math
  • Dynamically typed
  • Assign without declaring type, type can change
  • Semicolons suppress output

8
Vectors/ matrices I
  • gtgt B 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    16
  • B 1 2 3 4
  • 5 6 7 8
  • 9 10 11 12
  • 13 14 15 16
  • gtgt B(2,3)
  • ans 7
  • gtgt B(3,)
  • ans 9 10 11 12
  • gtgt B(24, 34)
  • ans 7 8 11 12 15 16
  • gtgt zeros(1,4) gtgt ones(2,2)

9
Vectors/ matrices II
  • gtgt A 14
  • A 1 2 3 4
  • gtgt A
  • ans 1
  • 2
  • 3
  • 4
  • gtgt C repmat(A,3,2)
  • C 1 2 3 4 1 2 3 4
  • 1 2 3 4 1 2 3 4
  • 1 2 3 4 1 2 3 4

10
Vectors/ matrices III
  • gtgt Dsum(B)
  • D 28 32 36 40
  • gtgt power(B(12,34),2)
  • ans 9 16
  • 49 64
  • gtgt EAB (concatenation)
  • gtgt B(2,)
  • B 1 2 3 4
  • 9 10 11 12
  • 13 14 15 16
  • gtgt (13)(46) VS gtgt (13).(46)

11
Vectors/ matrices continued
  • What MATLAB is optimized for
  • Everything is a matrix! (sort of)
  • Multi-variate data
  • Most functions work on matrices (more on this
    later)

12
Loading and saving
  • save/load to save/load workspace or
    variables/structures in .mat binary formatsave
    filenamesave(filename,var1,var2,)load
    filenameload filename X Y Z
  • dlmwrite/dlmread to write/read matrix to/from
    delimited ASCII filedlmwrite(filename,M,D)Md
    lmread(filename,D)

13
Graphing
  • gtgt x 0pi/20 2pi
  • gtgt y sin(x)
  • gtgt plot(x,y,b-x) blue line, Xs
  • gtgt hold keep lines
  • gtgt z cos(x)
  • gtgt plot(x,z,r) dotted red, s
  • gtgt hold release lines
  • gtgt dataimportdata(data_unsorted.txt)
  • gtgt plot(sort(data),(1128)/128) cdf

14
Control flow I
  • if any(Bgt5) NOT if Bgt5
  • true
  • else
  • false
  • end
  • for bB iterate over columns
  • 2b
  • End

15
You know those for loops I showed?
  • Never use them!
  • Or at least avoid when possible.
  • MATLAB meant for matrices-- vectorize code
  • P
  • for aA slow
  • P P (agt0)
  • end
  • P2 Agt0 fast

16
  • Original code
  • numAss 0 num assigned
  • numAssCorr 0 and should be
  • for I1numel(Actual)
  • if Assigned(i) 1
  • numAss numAss 1
  • if Actual(i) 1
  • numAssCorr numAccCorr 1
  • end end end
  • precision numAssCorr/numAss

17
  • Original code
  • numAss 0 num assigned
  • numAssCorr 0 and should be
  • for I1numel(Actual)
  • if Assigned(i) 1
  • numAss numAss 1
  • if Actual(i) 1
  • numAssCorr numAccCorr 1
  • end end end
  • precision numAssCorr/numAss
  • Vectorized code
  • numAss sum(Assigned)
  • numAssCorr sum( Assigned Actual )
  • precision numAssCorr/numAss

18
  • In m num of clusterings
  • n num points to be clustered, ngtgtm
  • E m X n set of clusterings, where
    E(i,j)cluster of point j in cluster i
  • Out C n X n similarity matrix, where C(j,k)
    number of i with E(i,j)E(i,k)
  • Naïve code (or, non-MATLAB code)
  • C zeros(n,n)
  • for i 1m
  • for j 1n
  • for k 1n
  • C(j,k) C(j,k) (E(i,j)E(i,k))
  • endendend
  • Can reorder for loops without affecting
    performance

19
  • In m num of clusterings
  • n num points to be clustered, ngtgtm
  • E m X n set of clusterings, where
    E(i,j)cluster of point j in cluster i
  • Out C n X n similarity matrix, where C(j,k)
    number of i with E(i,j)E(i,k)
  • Vectorize, try 1-- do all comparisons for one
    point at once
  • C should hoist allocation
  • for clustersE get clusters for
    one point
  • rep repmat(clusters,1,n) replicate
    clusters
  • C C sum(repE) compare to other
    points
  • end
  • Much better (3.3x faster on 20x1000)! Can we
    improve?

20
  • In m num of clusterings
  • n num points to be clustered, ngtgtm
  • E m X n set of clusterings, where
    E(i,j)cluster of point j in cluster i
  • Out C n X n similarity matrix, where C(j,k)
    number of i with E(i,j)E(i,k)
  • Vectorize, try 2-- ngtgtm, so iterate over m, not n
  • C zeros(n,n)
  • for clusteringE get one clustering
  • rep repmat(clustering,1,n) create copies
    of
  • clustering
  • C C (reprep) compare within
    clustering
  • end
  • Turned out to be slower on 20x1000, but consider
    changes like this.

21
Scripts and Functions
  • Save as .m files (and make sure in path)
  • See ethan/matlab_tutorial/ensemble1-3.m
  • _____
  • function C,time ensemble2( m, n, E )
  • tic
  • code from earlier slide
  • timetoc
  • _______
  • C2,t2ensemble2( 20, 1000, Ensemble)

22
Scripting MATLAB from Linux Shell
  • 2 options Ive used
  • 1) /projects/matlab/bin/matlab -nojvm -nodesktop
    -nosplash -nodisplay lt myscript.m
  • .m file specifies input and output
    values/filenames
  • 2) /projects/matlab/bin/matlab -nojvm -nodesktop
    -nosplash -nodisplay -r "A33dlmwrite('test.txt
    ',A,' ')quit"

23
Scripting MATLAB from Shell, II
  • ensemble2_wrap.m
  • Dims load(dims.txt')
  • E load(ensemble.txt)
  • Censemble2(Dims(1),Dims(2), E)
  • dlmwrite(similarity2.txt,C,' ')
  • quit
  • __________________________________
  • ethan/matlab_tutorial /projects/matlab/bin/matla
    b -nojvm -nodesktop -nosplash -nodisplay lt
    ensemble2_wrap.m

24
Scripting MATLAB from Shell, III
  • ensemble2_wrap2.m
  • function ensemble2_wrap2(dimsfn, ensfn,
    outfn)
  • Dims load(dimsfn)
  • Eload(ensfn)
  • Censemble2(Dims(1),Dims(2),E)
  • dlmwrite(outfn,C,' ')
  • _____________________________________
  • ethan/matlab_tutorial /projects/matlab/bin/matl
    ab -nojvm -nodesktop -nosplash -nodisplay -r
    "ensemble2_wrap2('dims.txt','ensemble.txt','simila
    rity2.txt')quit"

25
Weirdness and limitations
  • One-based indexing hard coded
  • Parentheses used for calling a function AND for
    indexing an array
  • No namespace resolution, so same code on
    different machines (w different path variables)
    can produce different results
  • Many functions have different behavior for
    matrices vs vectors (so base case broken)
  • No references
  • Limits data structures (linked list)
  • All functions call by value

26
MATLAB Functions
  • Tons implemented
  • Sometimes hard to find what you need
  • But great documentation once you do
  • help functionname
  • http//www.mathworks.com/access/helpdesk/help/help
    desk.html
  • Many additional found online
Write a Comment
User Comments (0)
About PowerShow.com