array accessing and strings in matlab
DESCRIPTION
Array Accessing and Strings in MATLAB. Creating Arrays / Chapter 2. Topics Covered: Array addressing. 2. Character arrays. 34- 35. >> v = [35 46 78 23 5 14 81 3 55] v = 35 46 78 23 5 14 81 3 55. >> v(4) ans = 23. >> v(7) ans = - PowerPoint PPT PresentationTRANSCRIPT
• ENG 1181
1
College of EngineeringEngineering Education Innovation Center
1
Array Accessing and Strings in MATLAB
Topics Covered:
1. Array addressing.
2. Character arrays.
Creating Arrays / Chapter 2
• ENG 1181
2
ARRAY ADDRESSING (VECTOR)
The address of an element in a vector is its position in the row (or column), starting at 1.
>> v = [35 46 78 23 5 14 81 3 55]v = 35 46 78 23 5 14 81 3 55
>> v(4) ans = 23
>> v(7) ans = 81
>> v(1) ans = 35
It is possible to change an element in a vector by entering a value to a specific address directly:
>> v(6) = 273v = 35 46 78 23 5 273 81 3 55
34- 35
• ENG 1181
3
Addressing Specified Elementsof a Vector
Any combination of elements from a vector can be addressed in any order
>> v = [4 15 8 12 34 2 50 23 11]
v =
4 15 8 12 34 2 50 23 11
>> u = v([4 5 7 3 3])
u =
12 34 50 8 8
36- 37
v([4 5 7 3 3]) is the same as ( [v(4), v(5), v(7), v(3), v(3)] ).
• ENG 1181
4
USING A COLON (:) WHEN ADDRESSING VECTORS
The colon operator can be used to generate a list of elements to address
All the elements of a vector (either a row vector or a
column vector)
Elements 3 through 7 ( [v(3), v(4), v(5), v(6), v(7)] ).
>> v = [4 15 8 12 34 2 50 23 11]
v =
4 15 8 12 34 2 50 23 11
>> u = v(3:7)
u =
8 12 34 2 50
36- 37
v(:)
v(3 : 7)
• ENG 1181
5
ARRAY ADDRESSING (MATRIX)
>> m=[3 11 6 5; 4 7 10 2; 13 9 0 8]
m =
3 11 6 5
4 7 10 2
13 9 0 8
>> m(1,1)
ans =
3
>> m(2,3)
ans =
10
35
Single elements can be used like variables in computations:
>> m(2,3) - m(1,1)ans = 7
• ENG 1181
6
USING A COLON (:) WHENADDRESSING MATRICES
A(: , 3)
A(2 , :)
A(: , 2:5)
A(2:4, :)
A(1:3, 2:4)
A([4 2],:)
36- 37
Elements in all the rows of column 3
Elements in all the columns of row 2
Elements in columns 2 through 5 in all the rows
Elements in rows 2 through 4 in all the columns
Elements in rows 1 through 3 and in columns 2
through 4
Elements in all the columns of rows 4 then 2
• ENG 1181
7
ADDRESSING MATRICES
>> 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 3 5 7 9
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
>> B = A(:,3)
B =
5
6
9
12
15
>> C = A(2,:)
C =
2 4 6 8 10
Define a matrix
36- 37
Define a matrixusing part of A
• ENG 1181
8
ADDRESSING MATRICES
>> D = A(:, 2:5)
D =
3 5 7 9
4 6 8 10
6 9 12 15
8 12 16 20
10 15 20 25
>> F = A(1:3,2:4)
F =
3 5 7
4 6 8
6 9 12
>> E = A(2:4,:)
E =
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
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
36- 37
>> G = A([5 2],5:-2:1)
G =
25 15 5
10 6 2
• ENG 1181
9
ADDRESSING MATRICES
>> A(1:2,1:3) = B([5 5],2:4)
A = 10 15 20 0 0
10 15 20 0 0
0 0 0 0 0
0 0 0 0 0
>> A = zeros (4,5)
A =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
36- 37
B =
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(3:4,[1 4 5]) = B(1:2,1:3)
A = 10 15 20 0 0
10 15 20 0 0
1 0 0 3 5
2 0 0 4 6
• ENG 1181
10
Strings are a special case of a vector (row array) 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: 'ad ef ', '3%fr2', '{edcba :21!'
Character Arrays
46- 48
• ENG 1181
11
STRING VARIABLES
>> a = 'ERty 8'
a =
ERty 8
>> B = ['My name is John Smith']
B =
My name is John Smith
a has 6 elements, and B has 21 elements
The elements can be addressed like any other row array
>> a(4)
ans =
y
>> B(12)
ans =
J
46- 48
• ENG 1181
12
STRING VARIABLES
>> x = 536
x =
536
>> x = '536'
x =
536
The string variable:
is not the same as the number variable:
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.
46- 48