CH 02
CH 02
CH 02
2
2.1 CREATING A ONE-DIMENSIONAL ARRAY (VECTOR)
5
2.1 CREATING A ONE-DIMENSIONAL ARRAY (VECTOR)
>> x = 1:2:13
x = 1 3 5 7 9 11 13
>> y = 1.5:0.1:2.1 Non-integer spacing
y = 1.5000 1.6000 1.7000
1.8000 1.9000 2.0000 2.1000
7
2.1 CREATING A ONE-DIMENSIONAL ARRAY (VECTOR)
>> z = -3:7
z = -3 -2 -1 0 1 2 3 4 5 6 7
>> xa = 21:-3:6 Negative spacing
xa = 21 18 15 12 9 6
8
2.1 CREATING A ONE-DIMENSIONAL ARRAY (VECTOR)
9
2.1 CREATING A ONE-DIMENSIONAL ARRAY (VECTOR)
10
2.2 CREATING A TWO-DIMENSIONAL ARRAY (MATRIX)
Mat =
3.0000 24.0000 0.5000
16.0000 1.6330 14.0000
12
2.2 CREATING A TWO-DIMENSIONAL ARRAY (MATRIX)
14
2.2.1 The zeros, ones and, eye Commands
15
2.2.1 The zeros, ones and, eye Commands
16
2.2.1 The zeros, ones and, eye Commands
17
2.3 NOTES ABOUT VARIABLES IN MATLAB
>> aa=[3 8 1]
aa = 3 8 1
>> bb=aa'
bb = 3
8
1
20
2.4 THE TRANSPOSE OPERATOR
21
2.5 ARRAY ADDRESSING
22
2.5.1 Vector
24
2.5.2 Matrix
>> MAT=[3 11 6 5; 4 7 10 2; 13 9 0 8]
Column 1
MAT = 3 11 6 5
Element in 4 7 10 2
row 3 and
column 1 13 9 0 8 Row 3
>> MAT(3,1)
ans = 13
>> MAT(3,1)=20 Assign new value to element in row 3 and column 1
MAT = 3 11 6 5
Only this 4 7 10 2
element
changed
20 9 0 8
>> MAT(2,4)-MAT(1,2)
ans = -9
26
2.6 USING A COLON : IN ADDRESSING ARRAYS
28
2.6 USING A COLON : IN ADDRESSING ARRAYS
29
2.6 USING A COLON : IN ADDRESSING ARRAYS
30
2.6 USING A COLON : IN ADDRESSING ARRAYS
>> v=4:3:34
v = 4 7 10 13 16 19 22 25 28 31 34
31
2.6 USING A COLON : IN ADDRESSING ARRAYS
32
2.7 ADDING ELEMENTS TO EXISTING VARIABLES
Appending to vectors
• Can only append row vectors to row
vectors and column vectors to column
vectors
If r1 and r2 are any row vectors,
r3 = [r1 r2] is a row vector whose left
part is r1 and right part is r2
If c1 and c2 are any column vectors,
c3 = [c1; c2] is a column vector whose
top part is c1 and bottom part is c2
35
2.7 ADDING ELEMENTS TO EXISTING VARIABLES
Appending to matrices
• If appending one matrix to right side of
other matrix, both must have same
number of rows
• If appending one matrix to bottom of
other matrix, both must have same
number of columns
38
2.7 ADDING ELEMENTS TO EXISTING VARIABLES
>> A2=[1 2 3; 4 5 6]
A2 = 1 2 3
4 5 6
>> B2=[7 8; 9 10]
B2 = 7 8
9 10
>> C2=eye(3)
C2 = 1 0 0
0 1 0
0 0 1
39
2.7 ADDING ELEMENTS TO EXISTING VARIABLES
41
2.8 DELETING ELEMENTS
42
2.9 BUILT-IN FUNCTIONS FOR HANDLING ARRAYS
45
2.10 STRINGS AND STRINGS AS VARIABLES
47
2.10 STRINGS AND STRINGS AS VARIABLES
In a string variable
• Numbers are stored as an array
• A one-line string is a row vector
Number of elements in vector is number of
characters in string
>> name = 'Howard the Duck';
>> size( name )
ans =
1 15
49
2.10 STRINGS AND STRINGS AS VARIABLES
50
2.10 STRINGS AND STRINGS AS VARIABLES
Example
>> word = 'dale';
>> word(1)
ans = d
>> word(1) = 'v'
word = vale
>> word(end) = []
word = val
>> word(end+1:end+3) = 'ley'
word = valley
51
2.10 STRINGS AND STRINGS AS VARIABLES
54
2.10 STRINGS AND STRINGS AS VARIABLES
EXAMPLE
>> question=char('Romeo,
Romeo,',...
'Wherefore art thou', 'Romeo?' )
question =
Romeo, Romeo,
Wherefore art thou
Romeo?
>> size( question )
ans =
3 18
55
2.10 STRINGS AND STRINGS AS VARIABLES
R o m e o , R o m e o ,
W h e r e f o r e a r t t h o u
R o m e o ?