Constructing a Simple Matrix
A = [row1; row2; ...; rown]
Ex: A = [12 62 93 -8 22; 16 2 87 43 91; -4 17 -72 95 6]
A =
12 62 93 -8 22
16 2 87 43 91
-4 17 -72 95 6
Creating a Magic Square Matrix
Ex: A = magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
Creating a Random Matrix
Ex: A = rand(3) * 20
A =
3.8686 10.8335 7.5675
13.6445 3.0175 17.2002
6.0553 13.9580 17.0731
Concatenating Matrices
Ex: A = ones(2, 5) * 6; B = rand(3, 5);
C = [A; B] ;
Replicating a Matrix
repmat(M, v, h)
Ex: A = [8 1 6; 3 5 7; 4 9 2];
B = repmat(A,1,2)
B =
8 1 6 8 1 6
3 5 7 3 5 7
4 9 2 4 9 2
Combining Single and Double Types
Ex: x = [single(4.5) single(-2.8) pi 5.73*10^300]
x =
4.5000 -2.8000 3.1416 Inf
class(x) % Display the data type of x
ans =
single
Combining Integer and Double Types
Ex: x = [int8(21) int8(-22) int8(23) pi 45/6]
x =
21 -22 23 3 7
class(x)
ans =
int8
Combining Character and Double Types
Ex: x = ['A' 'B' 'C' 68 69 70]
x =
ABCDEF
class(x)
ans =
char
Combining Logical and Double Types
Ex: x = [true false false pi sqrt(7)]
x =
1.0000 0 0 3.1416 2.6458
class(x)
ans =
double
Accessing Single Elements
For example, for a 3-by-3 magic square A
A =
8 1 6
3 5 7
4 9 2
you would access the element at row 3, column 2 with A(3, 2)
ans =
9
Linear Indexing
Ex: A = [2 6 9; 4 2 8; 3 0 1]
A = 2 6 9
4 2 8
3 5 1
is actually stored in memory as the sequence 2, 4, 3, 6, 2, 5, 9, 8, 1
Functions That Control Indexing Style
Ex: A = [2 6 9; 4 2 8; 3 0 1];
linearindex = sub2ind(size(A), 3, 2)
linearindex =
6
0 comments:
Post a Comment