ENGR 1187 | MATLAB 3: Array Accessing and Strings Preparation

advertisement
ENGR 1187 | MATLAB 3: Array Accessing and Strings
Preparation Material
Learning Objectives
1. Demonstrate proper notation for accessing elements from previously assigned onedimensional arrays (e.g., single elements, list of elements) and two-dimensional arrays (e.g.,
those with rows and columns)
2. Explain that a string is a one dimensional array and can be used the same way as numeric
arrays
Topics
Read Chapter 2.5 – 2.10 of the MATLAB book before coming to class. This preparation material
is provided to supplement this reading.
Students will learn how access arrays to either retrieve information or insert information, in
MATLAB. Creating strings, or text, will also be introduced. This material contains the following:
1.
2.
3.
4.
5.
Key Definitions
Array Addressing in Vectors
Array Addressing in Matrices
Array Accessing Examples
Character Arrays
1. Key Definitions
Vector – a list of numbers arranged in rows and/or columns.
Matrix – a two-dimensional array has numbers in both rows and columns.
2. Indexing in Vectors
Each element in a vector has an address that is defined by its position along the row or the column.
Each row or column is comprised of elements with addresses starting at 1.
Input the following to create (assign) the vector v:
>> v = [35 46 78 23 5 14 81 3 55]
v=
35
46
78
23
5
14
81
3
55
1
ENGR 1181 MATLAB 3: Array Accessing and Strings
Preparation Material
Elements within the vector v can then be addressed (or indexed) for display:
>> v(1)
>> v(4)
>> v(7)
ans =
35
ans =
23
ans =
81
It is possible to assign (change) an element in a vector by entering a value to a specific
address/index directly:
>> v(6) = 273
v=
35
46
78
23
5 273
81
3
55
Any combination of elements from a vector can be addressed in any order
v([4 5 7 3 3]) is the same as ( [v(4), v(5), v(7), v(3), v(3)] ).
The colon operator can be used to generate a list of elements to address
v( : )
All the elements of a vector (either a row vector or a column vector)
v( 3 : 7 )
Elements 3 through 7 ( [v(3), v(4), v(5), v(6), v(7)] )
2
ENGR 1181 MATLAB 3: Array Accessing and Strings
Preparation Material
>> u = v([4 5 7 3 3])
u=
23
5
81
78
78
3
ENGR 1181 MATLAB 3: Array Accessing and Strings
Preparation Material
4. Array Accessing (indexing) Examples
First, define a matrix A:
>> A = [1 3 5 7 9; 2 4 6 8 10; 3 6 9 12 15;
4 8 12 16 20; 5 10 15 20 25]
A=
1
2
3
4
5
3
4
6
8
10
5
6
9
12
15
7
8
12
16
20
9
10
15
20
25
Then, define a secondary matrix by referencing and using parts of matrix A:
>> B = A(:,3)
B=
5
6
9
12
15
>> C = A(2,:)
C=
2
4
6
8
10
>> D = A(:, 2:5)
D=
3
5
4
6
6
9
8 12
10 15
>> E = A(2:4,:)
7
8
12
16
20
9
10
15
20
25
E=
2
3
4
4
6
8
6
9
12
8
12
16
10
15
20
4
ENGR 1181 MATLAB 3: Array Accessing and Strings
Preparation Material
>> F = A(1:3,2:4)
F=
3 5
7
4 6
8
6 9 12
>> G = A([5 2],5:-2:1)
G=
25 15 5
10
6 2
5
ENGR 1181 MATLAB 3: Array Accessing and Strings
Preparation Material
5. Character Arrays
Strings are a special case of a vector (typically, a row vector) where every element is a character.
 Strings are created by entering the characters between single quotes.
 String arrays can include letters, digits, other symbols, and spaces
 Examples of strings:
‘Fred’ , ‘downtown’ , '1299 Neil Ave.’ , '99.55%', '75°F - 95°F’ , ‘{edcba :21!}'
>> a = 'ERty 8'
>> B = ['My name is John Smith']
a=
ERty 8
B=
My name is John Smith
NOTE: a has 6 elements, and B has 21 elements
The elements can then be addressed like any other row array
>> a(4)
>> B(12)
ans =
y
ans =
J
The string variable:
is not the same as the number variable:
>> x = '536'
>> x = 536
x=
536
x=
536
If you use a string variable in calculations, you get an answer based on the ascii storage code
for those characters (probably not what you wanted)!
An important application of strings is in creating input prompts and output messages. This will be
shown later when script file I/O (input/output) is discussed.
6
Download