MATLAB Programming
MATLAB Programming MATLAB Programming
1 Data Structures OperatingonDiagonalMatrices There are several MATLAB functions that work specifically on diagonal matrices. Function blkdiag diag trace tril triu Description Construct a block diagonal matrix from input arguments. Return a diagonal matrix or the diagonals of a matrix. Compute the sum of the elements on the main diagonal. Return the lower triangularpartofamatrix. Return the upper triangular part of a matrix. This section covers the following topics on diagonal matrices: • “Constructing a Matrix from a Diagonal Vector” on page 1-42 • “Returning a Triangular Portion of a Matrix” on page 1-43 • “Concatenating Matrices Diagonally” on page 1-43 Constructing a Matrix from a Diagonal Vector The diag function has two operations that it can perform. You can use it to generate a diagonal matrix: A = diag([12:4:32]) A = 12 0 0 0 0 0 0 16 0 0 0 0 0 0 20 0 0 0 0 0 0 24 0 0 0 0 0 0 28 0 0 0 0 0 0 32 You can also use the diag function to scan an existing matrix and return the values found along one of the diagonals: A = magic(5) A = 1-42
Operating on Diagonal Matrices 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 diag(A, 2) ans = 1 14 22 % Return contents of second diagonal of A Returning a Triangular Portion of a Matrix The tril and triu functions return a triangular portion of a matrix, the former returning the piece from the lower left and the latter from the upper right. By default, the main diagonal of the matrix divides these two segments. You can use an alternate diagonal by specifying an offset from the main diagonal as a second input argument: A = magic(6); B = tril(A, -1) B = 0 0 0 0 0 0 3 0 0 0 0 0 31 9 0 0 0 0 8 28 33 0 0 0 30 5 34 12 0 0 4 36 29 13 18 0 Concatenating Matrices Diagonally You can diagonally concatenate matrices to form a composite matrix using the blkdiag function. See “Creating a Block Diagonal Matrix” on page 1-10 for more information on how this works. 1-43
- Page 16 and 17: Features of Object-Oriented Program
- Page 18 and 19: Object Precedence .................
- Page 20 and 21: 12 Programming Tips Command and Fun
- Page 22 and 23: Converting Between Strings and Cell
- Page 24 and 25: A External Interfaces Finding the D
- Page 26 and 27: 1 Data Structures Empty Matrices, S
- Page 28 and 29: 1 Data Structures (shown as E below
- Page 30 and 31: 1 Data Structures 0 0 0 0 0 0 0 0 0
- Page 32 and 33: 1 Data Structures 0.6068 0.7621 0.8
- Page 34 and 35: 1 Data Structures 3 5 7 3 5 7 3 5 7
- Page 36 and 37: 1 Data Structures A = 3:0.2:3.8 A =
- Page 38 and 39: 1 Data Structures 5000 50 B = [int8
- Page 40 and 41: 1 Data Structures class(x) ans = si
- Page 42 and 43: 1 Data Structures Matrix Indexing T
- Page 44 and 45: 1 Data Structures A used in the pre
- Page 46 and 47: 1 Data Structures 16 5 9 4 . . . 12
- Page 48 and 49: 1 Data Structures Getting Informati
- Page 50 and 51: 1 Data Structures Function islogica
- Page 52 and 53: 1 Data Structures This example runs
- Page 54 and 55: 1 Data Structures A = 10 20 30 40 5
- Page 56 and 57: 1 Data Structures This changes matr
- Page 58 and 59: 1 Data Structures A = [1+9i 2-8i 3+
- Page 60 and 61: 1 Data Structures rp = rp + blocksi
- Page 62 and 63: 1 Data Structures A = [1:8; 11:18;
- Page 64 and 65: 1 Data Structures rand('state', 0);
- Page 68 and 69: 1 Data Structures Empty Matrices, S
- Page 70 and 71: 1 Data Structures These operations
- Page 72 and 73: 1 Data Structures Use the isscalar
- Page 74 and 75: 1 Data Structures Full and Sparse M
- Page 76 and 77: 1 Data Structures Multidimensional
- Page 78 and 79: 1 Data Structures As you add dimens
- Page 80 and 81: 1 Data Structures Generating Arrays
- Page 82 and 83: 1 Data Structures • size — Retu
- Page 84 and 85: 1 Data Structures For example, cons
- Page 86 and 87: 1 Data Structures Avoiding Ambiguit
- Page 88 and 89: 1 Data Structures Permuting Array D
- Page 90 and 91: 1 Data Structures Computing with Mu
- Page 92 and 93: 1 Data Structures • As multivaria
- Page 94 and 95: 1 Data Structures A{2,2} = 7; B{1,1
- Page 96 and 97: 1 Data Structures Summary of Matrix
- Page 98 and 99: 1 Data Structures Functions to Dete
- Page 100 and 101: 1 Data Structures 1-76
- Page 102 and 103: 2 Data Types Cell Arrays (p. 2-94)
- Page 104 and 105: 2 Data Types Data Type Summary The
- Page 106 and 107: 2 Data Types Numeric Types Numeric
- Page 108 and 109: 2 Data Types the two equally nearby
- Page 110 and 111: 2 Data Types x = x = 127 -128 Also,
- Page 112 and 113: 2 Data Types Set the warning state
- Page 114 and 115: 2 Data Types Floating-Point Numbers
Operating on Diagonal Matrices<br />
17 24 1 8 15<br />
23 5 7 14 16<br />
4 6 13 20 22<br />
10 12 19 21 3<br />
11 18 25 2 9<br />
diag(A, 2)<br />
ans =<br />
1<br />
14<br />
22<br />
% Return contents of second diagonal of A<br />
Returning a Triangular Portion of a Matrix<br />
The tril and triu functions return a triangular portion of a matrix, the<br />
former returning the piece from the lower left and the latter from the upper<br />
right. By default, the main diagonal of the matrix divides these two segments.<br />
You can use an alternate diagonal by specifying an offset from the main<br />
diagonal as a second input argument:<br />
A = magic(6);<br />
B = tril(A, -1)<br />
B =<br />
0 0 0 0 0 0<br />
3 0 0 0 0 0<br />
31 9 0 0 0 0<br />
8 28 33 0 0 0<br />
30 5 34 12 0 0<br />
4 36 29 13 18 0<br />
Concatenating Matrices Diagonally<br />
You can diagonally concatenate matrices to form a composite matrix using the<br />
blkdiag function. See “Creating a Block Diagonal Matrix” on page 1-10 for<br />
more information on how this works.<br />
1-43