You are on page 1of 8

Programing in Matlab

General rules of Matlab


 Comments symbol:%. Lines after % is nonexcutable.
 Block comments: %{ ... %}, this is for a block of or multiple lines of comments.
 space within code does no matter.
 lines with ; at the end, MATLAB does not display the result.
 Matlab is case sensitive.

1. Scalar operation and other common math functions


Example code for math operations/functions between single numbers in Matlab.
>> 3+5
ans =
8
>> 10-5
ans =
5
>> 4*3
ans =
12
>> 25/5
ans =
5
>> 4^2
ans =
16
>> sqrt(16) % square root of 16
ans =
4
>> nthroot(64,3) % the 3rd rood of 64
ans =
4
>> factorial(4) % 4!
ans =
24
>> exp(1) % exponential function: e^1
ans =
2.7183
>> exp(2) % exponential function: e^2
ans =
7.3891
>> log2(8) % logarithm with base 2
ans =
3
>> log10(100) % logarithm with base 10
ans =
2
>> log(8) % logarithm with base e
ans =
2.0794
>> log(exp(1)) % logarithm with base e
ans =
1
>> log(exp(2))
ans =
2
>> pi % pi ~ 3.1415926....
ans =
3.1416

Example of combination operations.

>> sqrt(2^2+5)
ans =
3
>> (8+6)/(8-1)
ans =
2
>> 22/(2+3^2)+(4/5)*(6/7)
ans =
2.6857

The precedence rules for MATLAB operators are shown in this list, ordered from highest precedence level to lowest precedence
level:

 Parentheses ()
 Transpose (.'), power (.^), complex conjugate transpose ('), matrix power (^)
 Power with unary minus (.^-), unary plus (.^+), or logical negation (.^~) as well as matrix power with unary minus (^-),
unary plus (^+), or logical negation (^~).
 Unary plus (+), unary minus (-), logical negation (~)
 Multiplication (.*), right division (./), left division (.\), matrix multiplication (*), matrix right division (/), matrix left
division (\)
 Addition (+), subtraction (-)
 Colon operator (:)
 Less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), not equal to (~=)
 Element-wise AND (&)
 Element-wise OR (|)
 Short-circuit AND (&&)
 Short-circuit OR (||)

2. Set value to variables.

>> x = sqrt(2^2+5)
x=
3
>> y = (8+6)/(8-1); % the value of y won't be displayed in command window, because of ; at the end
>> a = 3
a=
3
>> a = 3
a=
3
>> a = a*2 % value of variable can be overridden.
a=
6

3. Initial and create vectors, and vector operation

3.1 create and view vector.


There are two types of vector: row vector and column vector. There are three symbols which can be used to separate elements of
a vector: comma (,) , space, and semicolon (;). Comma (,) and space are used to create row vector, semicolon (;) is used to
create column vector. Examples are as following. Note that square bracket [] is usually used to create vectors.

>> v = [] % initial an empty vector

v=
[]

>> v = [1 0 0] % row vector, element should be separated by space or ,

v=
1 0 0

>> v = [1, 0 ,0] % row vector equivalent to above vector (line 52)

v=
1 0 0

>> v = [1; 2; 3] % column vector, element should be separated by ;

v=
1
2
3

>> u = v' % transpose a vector (row to column or column to row)

u=
1 2 3

We can also use the format [start:stepsize:end] to create a numeric vector, the increment of the elements are fixed, we call it
stepsize. See example below.
>> v = 1:0.5:3 % vector from 1 to 3, increased by 0.5. [start:stepsize:end]

v=
1.0000 1.5000 2.0000 2.5000 3.0000

>> v = -4:4 % stepsize is 1 by default

v=
-4 -3 -2 -1 0 1 2 3 4

We compare the following two vectors, we get different results because the operators’ precedence.

>> v1 = -4:4/4 % v1 = -4:4/4 = -4:1 , division ( / ) operation takes the higher priority

v1 =
-4 -3 -2 -1 0 1

>> v2 = [-4:4]/4 % [] take the higher priority

v2 =
-1.0000 -0.7500 -0.5000 -0.2500 0 0.2500 0.5000 0.7500 1.0000

Note that in the following code, v4 is equivalent to v2 above.

>> v3 = -4:4

v3 =
-4 -3 -2 -1 0 1 2 3 4

>> v4 = v3/4 % v4 is equivalent to v2

v4 =
-1.0000 -0.7500 -0.5000 -0.2500 0 0.2500 0.5000 0.7500 1.0000

3.2 vector operations.

>> a = [1 2 3 4]' % row or column vector? - column

a=
1
2
3
4

>> 2 * a % element level multiplication, multiply 2 to each element of a

ans =
2
4
6
8

>> a / 4 % element level division, divide each element of a by 4

ans =
0.2500
0.5000
0.7500
1.0000
>> b = [5; 6; 7; 8] % row or column vector? - column

b=
5
6
7
8

One vector plus/minus another vector (vectors should be the same type and same dimension). With our example of a and b:

>> a + b % vector addition, add up the corresponding elements at the same location of two vectors.

ans =
6
8
10
12

>> a - b % vector subtraction, subtract the corresponding elements at the same location of two vectors.

ans =
-4
-4
-4
-4

>> c = [5 6 7 8] % a row vector

c=
5 6 7 8

>> a + c % column vector + row vector => matrix

ans =
6 7 8 9
7 8 9 10
8 9 10 11
9 10 11 12

In the following example .^ indicate element level square. In our example:

>> a .^ 2 % elements level square (note the "."), square each element of vector a
ans =
1
4
9
16

>> a^2 % why get error? (vector * vector): can not do column vector * column vector
Error using ^
One argument must be a square matrix and the other must be a scalar. Use POWER (.^) for elementwise power.

Another element level multiplication: a .* b means multiply the element at the corresponding locations of vectors a, b.

>> a .* b % elements level multiplication (note the "."), multiply the corresponding elements at the same location
of two vectors.

ans =

5
12
21
32

>> a * b % why get error? column vector * column vector is not fine mathematically
Error using *
Inner matrix dimensions must agree.

Element level division: a ./ b means division at corresponding location of vector a and b.

>> a ./ b % elements level division (note the ".")

ans =

0.2000
0.3333
0.4286
0.5000

Some of the math functions only apply to element level, for example log().

>> log(a) % element level logarithm, logarithm function apply to each element of vector a.

ans =

0
0.6931
1.0986
1.3863
Note that vector/matrix multiplication always follow the dimension rule. Let’s say that vector/matrix A is m by n, another
vector/matrix B is p by q. We can do A * B only if n = p, that is the inner dimension should match. The resulted vector/matrix
has dimension m by q.

In the following example vector a is 4 by 1, vector b is 1 by 4. The inner dimensions are both 1. Hence we can do a*c. The
resulted matrix is 4 by 4.

>> a*c % 4 by 1 column vector * 1 by 4 row vector => 4 by 4matrix

ans =
2 2 2 2
4 4 4 4
6 6 6 6
8 8 8 8

With the same vectors. By switching their position, the inner dimensions are now both 4. Hence we can do c*a. The resulted
matrix is 1 by 1, which is a number.

>> c*a % 1 by 4 row vector * 4 by 1 column vector => one number

ans =

20

The following are some Matlab functions which can be applied to vector

>> sum(a) % sum of vector elements

ans =

10

>> mean(a) % mean of vector elements

ans =

2.5000

>> var(a) % variance of elements

ans =

1.6667

>> std(a) % standard deviation

ans =

1.2910

>> max(a) % maximum of vector elements


ans =

>> min(a) % minimum of vector elements

ans =

>> max(a)-min(a) % maximum value minus minimum value of a vector

ans =
3

You might also like