Richard's MatLab Page


Useful Links

 

Let's Start

Hey, you know how to install and run MatLab right? Let's skip this part. If you do not know, you can google it. It is too easy.
I use the MatLab 7 Getting Started Guide, which is free (really and legally) from the mathworks website.


Examples

Check out the

 

Chapter 1. Product Overview

Do I look like a salesman? Let's skip this part.

 

Chapter 2. Matrices and Arrays

Enter matrices into MATLAB in several different ways


Enter maxtrix
as a list of elements:

A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]

Use a semicolon, ; , to indicate the end of each row. It is automatically remembered in the MATLAB workspace


sum, transpose, and diag

sum(A) => sum of all columns

A' => transpose

diag(A) => main diagonal

fliplr(A) => flips a matrix from left to right


Subscripts

The element in row i and column j of A is denoted by A(i,j).

A(1,4) + A(2,4) + A(3,4) + A(4,4)

It is also possible to refer to the elements of a matrix with a single subscript, A(k). A(8) is another way of referring to the value 15 stored in A(4,2). Conversely, if you store a value in an element outside of the matrix, the size increases to accommodate the newcomer:


The Colon Operator

1:10 => row vector containing the integers from 1 to 10:
1 2 3 4 5 6 7 8 9 10

100:-7:50 => nonunit spacing
100 93 86 79 72 65 58 51

0:pi/4:pi
0 0.7854 1.5708 2.3562 3.1416

A(1:k,j) => first k elements of the jth column of A. Thus:
sum(A(1:4,4))

The colon by itself refers to all the elements in a row or column of a matrix and the keyword end refers to the last row or column.
sum(A(:,end)) => computes the sum of the elements in the last column of A

sum(1:16)


The magic Function

creates magic squares of almost any size: magic(4)


Expressions

These expressions involve entire matrices.


Variables

num_students = 25 => creates a 1-by-1 matrix

Variable names consist of a letter, followed by any number of letters, digits, or underscores. MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. MATLAB uses only the first N characters of the name, (where N is the number returned by the function namelengthmax), and ignores the rest.


Numbers

3
-99
0.0001
9.6397238
1.60210e-20
6.02252e23
1i
-3.14159j
3e5i

Imaginary numbers (complex number whose squared value is a real number not greater than zero, i think here only means complex number) use either i or j as a suffix.

sort([3+4i, 4+3i])
angle(3+4i)


Operators

+ Addition
- Subtraction
* Multiplication
/ Division
\ Left division (described in “Linear Algebra” in the MATLAB documentation)
^ Power
' Complex conjugate transpose
( ) Specify evaluation order


Functions

e.g. abs, sqrt, exp, and sin.
help elfun
=> elementaty maths functions
help specfun => advanced maths functions
help elmat => matrix functions

pi 3.14159265...
i Imaginary unit,
j Same as i
eps Floating-point relative precision,
realmin Smallest floating-point number,
realmax Largest floating-point number,
Inf Infinity
NaN Not-a-number


Generating Matrices

zeros All zeros
ones All ones
rand Uniformly distributed random elements
randn Normally distributed random elements

zeros(2,4)

ones(3,3)

rand(1,10)
fix(10*rand(1,10))

randn(4,4)


The load Function
- Load content from file

The load function reads binary files containing matrices generated by earlier MATLAB sessions, or reads text files containing numeric data. The text file should be organized as a rectangular table of numbers, separated by blanks, with one row per line, and an equal number of elements in each row.

Steps:

  1. outside of MATLAB, create a text file containing these four lines (for a matrix):
    16.0 3.0 2.0 13.0
    5.0 10.0 11.0 8.0
    9.0 6.0 7.0 12.0
    4.0 15.0 14.0 1.0
  2. Save the file as magik.dat in the current directory.
  3. The statement
    load magik.dat
    reads the file and creates a variable, magik, containing the example matrix.


The save Function - Save workspace variables to disk.

SAVE FILENAME X - save variable X to filename

save test.txt magik -ASCII - save variable magik to filename text.txt with ASCII option

ASCII Options:
SAVE ... -ASCII uses 8-digit ASCII form instead of binary regardless
of file extension.
SAVE ... -ASCII -DOUBLE uses 16-digit ASCII form.
SAVE ... -ASCII -TABS delimits with tabs.
SAVE ... -ASCII -DOUBLE -TABS 16-digit, tab delimited.

Deleting rows and Columns

Then, to delete the second column of X, use
X(:,2) = []