Professional Documents
Culture Documents
Dr. Tu Le
School of Engineering
RMIT University, Victoria, Australia
Email: tu.le@rmit.edu.au
Scalar Product of Two Vectors
2
Scalar Product of Two Vectors
3
Scalar Product of Two Vectors
4
5
Dot Product and Dot Division
• .* : multiplying element-by-element
• ./ : dividing element-by-element
Both dot product and dot division only apply
when two same-length vectors or two same-size
matrices are involved.
>> w=[2 1 3]; z=[7;6;5];
>> w.*w
ans = 4 1 9
>> w.*z'
ans = 14 6 15
6
Dot Product and Dot Division
7
Dot Product and Dot Division
8
What is echoed by Matlab command line?
9
Dot Power
13
Deleting Rows and Columns
14
Operations involving scalars
16
Priorities
17
Variable Names
18
Suppressing Output
19
Mathematical Built-In Functions
20
Mathematical Built-In Functions
21
Logical Subscripts
– >> A = rand(50,1);
– >> I = (A>0.5);
– >> B = A(I);
– >> C= A(A>0.5);
• A is created as a vector containing 50 random numbers
between 0 and 1.
• I will contain a 50-by-1 vector of zeros or ones. Each element
is one the corresponding element in A is greater than 0.5,
otherwise it is zero.
• B will contain those elements of A that correspond to ones in
I, i.e. the elements that are greater than 0.5.
• C will be exactly the same as B. It is just created directly
from A without separately building the indices vector I.
22
Logical Subscripts
– >> A = rand(50,1);
– >> I = (A>0.5);
– >> B = A(I);
– >> C= A(A>0.5);
• What is the size of C?
• You can use the “size”
command.
– >> [m,n] = size(C);
• This will create two
variables in the
workspace, m and n,
where the number of
rows and columns in C
23 will be saved.
Logical Subscripts
24
Logical Subscripts
25
Common Data Types in MATLAB
26
Some Examples to Recap
>> v = [1 3, sqrt(5)]
v = 1.0000 3.0000 2.2361
>> length(v)
ans = 3
>> v2 = [3+ 4 5]
v2 = 7 5
>> v3 = [3 +4 5]
v3 = 3 4 5
>> v+v3
ans = 4.0000 7.0000 7.2361
>> v4 = 3*v
v4 = 3.0000 9.0000 6.7082
>> v5 = 2*v -3*v3
v5 = 7.0000 -6.0000 -10.5279
>> v+v2
??? Error using ==> plus
27
Matrix dimensions must agree.
Some Examples to Recap
>> w = [1 2 3], z = [8 9]
w = 1 2 3
z = 8 9
>> cd=[2*z,-w], sort(cd)
cd = 16 18 -1 -2 -3
ans = -3 -2 -1 16 18
>> w(2) = -2, w(3)
w = 1 -2 3
ans = 3
28
Some Examples to Recap
>> 1:4
ans = 1 2 3 4
>> 3:7
ans = 3 4 5 6 7
>> 1:-1
ans = Empty matrix: 1-by-0
>> 0.32:0.1:0.6
ans = 0.3200 0.4200 0.5200
>> -1.4:-0.3:-2
ans = -1.4000 -1.7000 -2.0000
>> r5 = [1:2:6,-1:-2:-7]
r5 = 1 3 5 -1 -3 -5 -7
>> r5(3:6)
ans = 5 -1 -3 -5
>> r5(1:2:7)
ans = 1 5 -3 -7
>> r5(6:-2:1)
29
ans = -5 -1 3
Some Examples to Recap
30
Some Examples to Recap
>> w = [1 -2 3]
w = 1 -2 3
>> w'
ans = 1
-2
3
>> c=[1; 2; 3]
c = 1
2
3
>> c'
ans =1 2 3
31
Some Examples to Recap
>> A = ones(2,3)
>> c=[0 1;3 -2;4 2], x=[8;-4;1]
A = 1 1 1
c = 0 1
1 1 1
3 -2
>> B = zeros(1,3)
4 2
B = 0 0 0
x = 8
>> C = ones(size(B))
-4
C = 1 1 1
1
>> D = eye(3)
>> g=[c x]
D = 1 0 0
g = 0 1 8
0 1 0
3 -2 -4
0 0 1
4 2 1
>> d=[-3 4 2]
d = -3 4 2
>> D=diag(d)
D = -3 0 0
0 4 0
32
0 0 2
Some Examples to Recap
>> j=[1:4;5:8;9:12;20 0 5 4]
j =
1 2 3 4
5 6 7 8
9 10 11 12
20 0 5 4
>> k=[diag(1:4) j;j' zeros(4,4)]
k =
1 0 0 0 1 2 3 4
0 2 0 0 5 6 7 8
0 0 3 0 9 10 11 12
0 0 0 4 20 0 5 4
1 5 9 20 0 0 0 0
2 6 10 0 0 0 0 0
3 7 11 5 0 0 0 0
4 8 12 4 0 0 0 0
34
Some Examples to Recap
>> j=[1:4;5:8;9:12;20 0 5 4]
j =
1 2 3 4
5 6 7 8
9 10 11 12
20 0 5 4
>> j(4,1)=j(1,1)+6
j =
1 2 3 4
5 6 7 8
9 10 11 12
7 0 5 4
>> j(1,1)=j(1,1)-3*j(1,2)
j =
-5 2 3 4
5 6 7 8
9 10 11 12
36
7 0 5 4