19.2 Visualising 2D Arrays in Matlab
• One common view of
2D arrays is:
- a series of numbered boxes
- boxes are arranged into rows and
columns and are numbered on that basis
- each box may hold a single data
value
- each particular box is addressed
by using its row and column number
- the collection of boxes together
has the name of the array
19.3 Visualising 2D Arrays Example
• For example a
table of network traffic collected hourly over several days:
- the array (matrix) is called
traffic
- it has 7 rows (corresponding to
the days of the week), and 24 columns (corresponding to the hours of the day)
- hence it has 168 elements
- element number
(2,23) [row 2, column 23] contains the value 11.8
19.4 Creation
• Creation of 2D
arrays follows the same principles as for vectors:
- square brackets
- spaces or commas
to separate values on a row
- semi-colon or <returns> to
separate rows
• For example:
>> square2 = [ 1 2 ; 3 4]
square2 =
1
2
3
4
19.5 Creation Example
>> firstLine = 1:5
firstLine =
1
2 3 4
5
>> secondLine
= 6:10
secondLine =
6
7 8 9
10
>> rect2 =
[firstLine ; secondLine]
rect2 =
1
2 3 4
5
6
7 8 9
10
>> rect5 = rect2'
rect5 =
1
6
2
7
3
8
4
9
5
10
>> diary off
19.6 Mathematics of Matrices
• Mathematical
operations with matrices follow the rules previously presented for vectors:
- operations with scalars are
applied individually to elements of the matrix
- operations involving two
matrices follow the mathematical definition for that operation on two
matrices
- special “dot” operators have
been provided to allow element by element operations for two matrices
• For example:
>> square2
square2 =
1
2
3
4
>> 2-square2
ans =
1
0
-1
-2
>> 10*square2
ans =
10 20
30 40
19.7 Mathematics Example
>>
long=[square2 square2*10 square2(:,1)*100]
long =
1
2 10 20
100
3
4 30 40
300
>> rect2
rect2 =
1
2 3 4
5
6
7 8 9
10
>> sum =
long+rect2
sum =
2
4 13 24
105
9
11 38 49
310
>> 2.^square2
ans =
2
4
8
16
Mathematics Example
(Cont)
>> rect5
rect5 =
1
6
2
7
3
8
4
9
5
10
>> rect2*rect5
ans =
55
130
130
330
>> rect5*rect2
ans =
37
44 51 58
65
44
53 62 71
80
51
62 73 84
95
58
71 84 97
110
65
80 95 110
125
>> long./rect2
ans =
1.0000
1.0000 3.3333 5.0000
20.0000
0.5000
0.5714 3.7500 4.4444
30.0000
>> long/rect2
ans =
29.2400
-7.6400
85.7200
-22.7200
Mathematics Example
(Cont)
>> format
short g
>> long.^rect2
ans =
1 4 1000 1.6e+05 1e+10
729 16384 6.561e+11 2.6214e+14 5.9049e+24
>> diary off
19.8 Addressing & Manipulation
• Elements of a
matrix are specified using:
- name of the matrix and circle
brackets
- row and column number(s)
(separated by comma) of elements to be addressed.
|
• For example:
>> ex1 = [1 4
7; 2 5 8; 3 6 9]
ex1 =
1
4 7
2
5 8
3
6 9
>> ex2 = ex1'
ex2 =
1
2 3
4
5 6
7
8 9
>> ex2(2,3)
ans =
6
>> ex1(2,3)
ans =
8
19.9 Addressing & Manipulation Example
>> ex1(2,2)=-5
ex1 =
1
4 7
2
-5 8
3
6 9
>> ex1(2,5) =
-5
ex1 =
1
4 7 0
0
2
-5 8 0
-5
3
6 9 0
0
>>
ex1(:,4)=[10 11 12]
??? In an assignment A(:,matrix) = B, the number of elements in
the subscript of A and the number
of columns in B must
be the same.
|
KURSUS MATLAB ONLINE Skripsi, Tesis, DISERTASI 081219449060
Selasa, 13 Januari 2015
Visualising 2D Arrays in Matlab - KURSUS MATLAB ONLINE Skripsi, Tesis, DISERTASI 081219449060
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar