Introduction to MATLAB

  • Matrix Lab
  • from MathWorks
  • Developed on late 1970’s
  • Latest Release : R2013b
    • MATLAB 8.2
  • Interpreted Language for Numerical Computation
  • Dynamically Typed with Case-Sensitive syntax

How to get it ?

  • Ask your department for a license
  • Buy the Student Version
    • Software Center in Torgersen Bridge
  • try GNU Octave from here
    • an open source mathematics software with syntax similar to MATLAB

Working with Scalars, Vectors and Matrices

  • Scalars, Vectors = Matrices
  • Defining Variables
    • naming rules
    • new objects are created by passing value

Data Types and Casting

  • integer

    • signed integers: int8,int16,int32,int64
    • unsigned integers: uint8,…
  • float

    • Single, Double
  • logical

    • true (= 1),false (= 0)
  • special

    • NaN, Inf

Working with Scalars, Vectors and Matrices

  • Creating Matrices:
    • typing in all the elements
    • ones(), zeroes(), rand(), eye(), randi()
    • ones(m,n)
    • m: number of rows
    • n: number of columns

Working with Scalars, Vectors and Matrices

  • Output to display
    • printf("x= %f",x)
    • display(x)
    • x

Accessing the elements of a Matrix

  • index starts at 1
    • A(1,1) is the first element
  • : operator
    • A(1:end:2)
  • rows and columns can be added to a Matrix

Working with Scalars, Vectors and Matrices

  • formatting floating point data
    • format long/short/longE/shortE
  • Suppressing the output with ;
  • rounding/chopping
    • round(), fix(), ceil(), floor()

Working with Scalars, Vectors and Matrices

  • Reading the documentation on a specific command
    • help "command"
    • doc "command"
    • Make use of Matlab documentation !

Basic Arithmetic Operations

  • Element-wise operations
    • +
    • -
    • .^
    • ./
    • .*

Basic Operations

  • Matrix operations:
    • A * B
    • A ^ n
    • transpose(A),conjugate(A)`
    • A'

Scripts

  • M-files
    • Write your program line by line, then save as “ProgName.m”
    • use Comments to clarify your code
      • % like this

Program Flow Control

  • if statement
  • for loop
  • while statement

if (condition)

statements;

elseif (condition)

statements;

else

statements;

end

while (condition)

statements;

end

for i=index_Matrix

statements;

end

Functions

  • also M-files, with the following definition

function [y1,...,yN] = myfun(x1,...,xM)

function body statements;

end

Data Visualization

  • plot(X,Y,'r-x')
  • semilogx() , loglog()
  • pie(), bar(), etc…

Timing your program

  • tic and toc
  • Matlab Profiler

Tips for writing fast programs

  • especially when dealing with large data
  • look for a dedicated function in Matlab documentation
  • avoid nested loops
  • stick to matrix manipulation commands
    • eg [r,c]=find(A>3)