Professional Documents
Culture Documents
>> 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 (||)
>> 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
v=
[]
v=
1 0 0
>> v = [1, 0 ,0] % row vector equivalent to above vector (line 52)
v=
1 0 0
v=
1
2
3
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 -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 =
-1.0000 -0.7500 -0.5000 -0.2500 0 0.2500 0.5000 0.7500 1.0000
>> v3 = -4:4
v3 =
-4 -3 -2 -1 0 1 2 3 4
v4 =
-1.0000 -0.7500 -0.5000 -0.2500 0 0.2500 0.5000 0.7500 1.0000
a=
1
2
3
4
ans =
2
4
6
8
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
ans =
6 7 8 9
7 8 9 10
8 9 10 11
9 10 11 12
>> 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.
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.
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.
ans =
20
The following are some Matlab functions which can be applied to vector
ans =
10
ans =
2.5000
ans =
1.6667
ans =
1.2910
ans =
ans =
3