Matlab Lecture 1 & 2
Matlab Lecture 1 & 2
Learning Objectives:
Command window
• Main window – where commands are entered
Vectors and Matrices
Variables
ALL variables are matrices
>> B = [10;12;14;16;18]
B = 10
10
A column
12
12
vector –
14 values are
separated
B 14
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
>> 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
>>> 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
1x1 2 x1 3x1 1 2 3
4 x 2 5x 2 6 x 2 8 10 12
A.*B =
7 x 3 8x 3 9x 3 21 24 27
Vectors and Matrices
11 21 31 1 2 3
A.^B 2
4 52
62
=
16 25 36
73 83 93 343 512 729