§1 Basic ConceptsSome ShortcutsSome TipsNumerical MatricesVariablesArithmetic Expressions and Mathematical Functions§2 Matrix OperationsBasic OperationsMatrix Functions and OperatorsMatrix Creation

§1 Basic Concepts

Some Shortcuts

Some Tips

  1. You are allowed to define more than one variable on the same line, provided they are separated by ';'.

  2. You can input '...' at the end of the line in order to continue typing on the next line.

  3. In MATLAB, The elements of the matrix are indexed by column first and then row.

  4. Date Formats

    NumbersForms (BACK)
    022-Apr-2020 02:14:00
    122-Apr-2020
    204/22/20
    3Apr
    4A (April)
    54 (April)
    604/22
    722
    8Thu (Thursday)
    9T (Thursday)
    102020
    1120 (2020)
    12Apr20
    1302:14:00
    1402:14:00 AM
    1502:14
    1602:14 AM
    17Q2-20 ( quarters of 1 hour-2020)
    18Q2
  5. Output Formats

    1. Normally, integer or short float with 4 decimal places

    2. Format Changes

  6. Help Commands

Numerical Matrices

  1. 2-dim Matrices

    row: separated by ' ' or ','

    column: separated by ';' or Enter

  2. Multi-dim Matrices

  3. Dimensions and Lengths

    1. size(#)

    2. length(#)

    3. ndims(#)

  4. Conversion between Subscripts and indexes

    1. sub2ind

    2. ind2sub

Variables

  1. Some Basic Rules

    1. In MATLAB, variables are case sensitive and the predefined are usually lowercase.
    2. 'A~Z', 'a~z' and '_' can be used to name a variable, but the first char of the name must be a letter.
  2. Simple Data Types

    TypesExplanations
    doubleDouble-precision floating-point format/64-digit
    char16-digit
    sparseTo storage sparse matrix
    unit88-digit unsigned int
  3. Logical Functions

    FunctionsExplanations
    iscell(x)If true, return 1; Else return 0/Cell matrix
    isfield(x)Ditto
    isfinite(x)Return the same vector as x
    islogical(x)Same as /Logical vector
    isnumeric(x)Ditto/Numeric vector
    isstr(x)Ditto/String
    isstruct(x)Ditto/Structure
    isobject(x)Ditto/Object
    logical(x)Return a logical vector that can be used
  4. Predefined Variables

    VariablesExplanations
    ansassigned the value of the last expression, which doesn't have a name
    epsthe diff between 1 and the closest representative floating-point number
    You can set a new value, but it can't restore by the command 'clear'.
    realmax realminthe biggest/smallest float-point number
    pinamely
    infdefined as
    When 0 becomes the divisor, it will return 'inf' instead of interruption.
    NaNdefined as 'Not a Number'
    It is '%'type or
    i jdefined as
    You can set new values for them and restore by the command 'clear'.
  5. View Variables

    commandsExplanations
    whoreturn all the defined variables
    who globalreturn all the defined global variables
    who a*return all the defined variables which start with 'a'
    whos whos globalshow more details than who/who global
    exist(namestr)return diff values according to the definitions of variables in 'namestr'
    inmemreturn a cell vector, which includes functions and M files in memory so far
    workspaceopen 'workspace'
  6. Delete Variables

    commandsExplanations
    cleardelete all the defined variables and restore all the predefined except eps
    clear name1 name2 ...delete the specific variables
    clear a*delete all the defined variables which start with 'a'

    2 equivalent descriptions:

    command variable

    command('variable')

Arithmetic Expressions and Mathematical Functions

  1. Arithmetic Operators

    PriorityOperators
    H^
    M* / \
    L+ -
  2. Mathematical Functions

    FunctionsExplanations
    abs(x)absolute value
    sign(x)sign function
    sqrt(x)
    pow2(x,f)
    exp(x)
    log(x)
    log10(x)
    sin(x)...trigonometric functions

    The x in trigonometric functions must be in radians.

  3. Rounding and Relevant Commands

    CommandsExplanations
    round(x)return the integer closest to x
    fix(x)return the integer closest to x towards 0
    floor(x) ceil(x)round down/up
    rem(x,y)return the remainder of x/y
    gcd(x,y)GCD
    [a,b,c]=gcd(x,y)
    lcm(x,y)LCM
    [t,n]=rat(x) is the approximate value of x with a relative error less than
    [t,n]=rat(x,tol)... less than 'tol'
    rat(x) rat(x,tol)return continued fraction form of x with a relative error
  4. Functions on Complex Numbers

    FunctionsExplanations
    real(z) imag(z)return the real/imaginary part of z
    abs(z)absolute value
    conj(z)return the complex conjugate of z
    angle(z)return the phase angle of z
    unwrap(v) unwrap(v,k)correct the angle to make the diff of adjacent elements smaller than /
    cplxpair(v)return complex conjugate pairs
  5. Coordinate Conversions

    ConversionsExplanations
    [theta,r]=cart2pol(x,y)convert Cartesian coordinate system into polar coordinate system
    [x,y]=pol2cart(theta,r)convert polar coordinate system into Cartesian coordinate system
    [alpha,theta,r]=cart2sph(x,y,z)convert Cartesian coordinate system into spherical coordinate system
    [x,y,z]=sph2cart(alha,theta,r)converse process of the above
  6. Flops and Time Management

    1. flops

        1. real: 1 operation
        2. complex: 2 operations
        1. real: 1 operation
        2. complex: 6 operations
      1. Elementary Functions

        1. real: 1 operation
        2. complex: more than 1
      2. flops(0) reset to 0

    2. Time Management

      CommandsExplanations
      ticstart a timer
      tocread the time of the timer started by tic
      clock
      fix(clock)
      return the date and time (scientific notation)
      etime(t1,t2)return the seconds between t1 and t2
      eputimereturn the seconds of CPU since startup of MATLAB
      CommandsExplanations
      datedate-month-year
      calendar(yyyy,mm)return the appointed calendar
      datenum(yyyy,mm,dd) day: 0000-01-01
      datestr(d,form)return the date denoted by form
      datetick(axis,form)write data on the coordinate axis in the figure
      datevec(d)return [yyyy mm dd ho mi se]
      enomday(yyyy,mm)the number of days
      nowsimilar to datenum
      [daynr dayname]=weekday(day)return the day of the week (1 is Sunday)

§2 Matrix Operations

Basic Operations

  1. Multiplication

    1. 2-dim only

    2. similar as dot product

    3. Convolution

    4. Tensor Product

  2. Division inverse matrix: inv(Matrix_name)

  3. Conjugate and Transpose

    1. Conjugate: A.' or conj(A)
    2. Conjugate and Transpose: A'
  4. Exponentiation

  5. Element Operations + - .* ./ .\ .^

Matrix Functions and Operators

  1. Matrix Functions

    Functions
    expm(Matrix_name)
    logm(Matrix_name)
    sqrtm(Matrix_name)

    Note the difference between expm and exp etc.

  2. Relational Operators < <= > >= == ~= The elements of the result matrix are 0(false) or 1(true).

  3. Logical Operators & | ~ xor

  4. Logical Functions

    1. find to give the values and positions of adequate elements of the matrix or vector

    2. any all return Boolean values

    3. Some Others

      FunctionsExplanations
      isnan(Matrix_name)NaN: 1 others: 0
      isinf(Matrix_name)inf: 1 others: 0
      isempty(Matrix_name)empty: 1 else: 0
      isequal(Matrix_name1,Matrix_name2)equal: 1 else: 0
      isreal(Matrix_name)real: 1 else: 0
      isfinite(Matrix_name)finite: 1 others: 0

Matrix Creation

  1. Basic Matrix Establishment

    1. 1-matrix, 0-matrix and Unit Matrix

    2. Random Matrix

      1. rand to generate random numbers between 0 and 1 evenly

      2. randn similar to rand, normally distributed random number with unit variance and zero mean