Lec 1,2
Lec 1,2
Learning Objectives:
Command window
• Main window – where commands are entered
Vectors and Matrices
Variables
ALL variables are matrices
A 1 2 3 4 5
>> A = [1 2 3 4 5] A row vector –
A = values are
1 2 3 4 5 separated by
>> spaces
>> B = [10;12;14;16;18]
B = 10
10 12
12
A column
vector –
14 values are B 14
separated
16 by semi–
18 colon (;) 16
>> 18
If we want to construct a vector of, say, 100
elements between 0 and 2 – linspace
>> c1 = linspace(0,(2*pi),100);
>> whos
Name Size Bytes Class
c1 1x100 800 double array
Grand total is 100 elements using 800 bytes
>>
If we want to construct an array of, say, 100
elements between 0 and 2 – colon notation
>> c2 = (0:(2*pi)/99:(2*pi))
>> whos
Name Size Bytes Class
c1 1x100 800 double array
c2 1x100 800 double array
Grand total is 200 elements using 1600 bytes
>>
• How do we assign values to matrices ?
>> D = (0:5)
D=
0 1 2 3 4 5 >> D (3)
ans =
2
Vectors and Matrices
• Some special variables
>> 1/0
Warning: Divide by zero.
beep ans =
pi () Inf
>> pi
inf (e.g. 1/0)
ans =
i, j ( 1 ) 3.1416
>> i
ans =
0+ 1.0000i
Vectors and Matrices
• Arithmetic operations – Matrices
>>> A/3
ans =
0.3333 0.6667 1.0000
1.3333 1.6667 2.0000
2.3333 2.6667 3.0000
Vectors and Matrices
1 2 3 1 1 1 14 14 14
4 5 6 2 2 2 32 32 32
=
A*B 7 8 9 3 3 3 50 50 50
11 21 31 1 2 3
A.^B 2 2 2
= 16 25 36
4 5 6
73 83 93 343 512 729