MATLAB Mathematics - SERC - Index of
MATLAB Mathematics - SERC - Index of MATLAB Mathematics - SERC - Index of
6 Sparse Matrices simply sparse(m,n). The sparse analogs of rand and eye are sprand and speye, respectively. There is no sparse analog for the function ones. • Unary functions that accept a matrix and return a matrix or vector preserve the storage class of the operand. If S is a sparse matrix, then chol(S) is also a sparse matrix, and diag(S) is a sparse vector. Columnwise functions such as max and sum also return sparse vectors, even though these vectors may be entirely nonzero. Important exceptions to this rule are the sparse and full functions. • Binary operators yield sparse results if both operands are sparse, and full results if both are full. For mixed operands, the result is full unless the operation preserves sparsity. If S is sparse and F is full, then S+F, S*F, and F\S are full, while S.*F and S&F are sparse. In some cases, the result might be sparse even though the matrix has few zero elements. • Matrix concatenation using either the cat function or square brackets produces sparse results for mixed operands. • Submatrix indexing on the right side of an assignment preserves the storage format of the operand unless the result is a scalar. T = S(i,j) produces a sparse result if S is sparse and either i or j is a vector. It produces a full scalar if both i and j are scalars. Submatrix indexing on the left, as in T(i,j) = S, does not change the storage format of the matrix on the left. • Multiplication and division are performed on only the nonzero elements of sparse matrices. Dividing a sparse matrix by zero returns a sparse matrix with Inf at each nonzero location. Because the zero-valued elements are not operated on, these elements are not returned as NaN. Similarly, multiplying a sparse matrix by Inf or NaN returns Inf or NaN, respectively, for the nonzero elements, but does not fill in NaN for the zero-valued elements. Permutation and Reordering A permutation of the rows and columns of a sparse matrix S can be represented in two ways: • A permutation matrix P acts on the rows of S as P*S or on the columns as S*P'. • A permutation vector p, which is a full vector containing a permutation of 1:n, acts on the rows of S as S(p,:), or on the columns as S(:,p). 6-26
Sparse Matrix Operations For example, the statements p = [1 3 4 2 5] I = eye(5,5); P = I(p,:); e = ones(4,1); S = diag(11:11:55) + diag(e,1) + diag(e,-1) produce p = P = S = 1 3 4 2 5 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 11 1 0 0 0 1 22 1 0 0 0 1 33 1 0 0 0 1 44 1 0 0 0 1 55 You can now try some permutations using the permutation vector p and the permutation matrix P. For example, the statements S(p,:) and P*S produce ans = 11 1 0 0 0 0 1 33 1 0 0 0 1 44 1 1 22 1 0 0 0 0 0 1 55 6-27
- Page 216 and 217: 5 Differential Equations Note The d
- Page 218 and 219: 5 Differential Equations legend('An
- Page 220 and 221: 5 Differential Equations Here, v(1-
- Page 222 and 223: 5 Differential Equations solution v
- Page 224 and 225: 5 Differential Equations Note The D
- Page 226 and 227: 5 Differential Equations After disc
- Page 228 and 229: 5 Differential Equations The output
- Page 230 and 231: 5 Differential Equations 2 Code the
- Page 232 and 233: 5 Differential Equations Note See t
- Page 234 and 235: 5 Differential Equations The exampl
- Page 236 and 237: 5 Differential Equations and the ri
- Page 238 and 239: 5 Differential Equations u1(x,t) 1
- Page 240 and 241: 5 Differential Equations Selected B
- Page 242 and 243: 6 Sparse Matrices Function Summary
- Page 244 and 245: 6 Sparse Matrices Function Summary
- Page 246 and 247: 6 Sparse Matrices This matrix requi
- Page 248 and 249: 6 Sparse Matrices S = (3,1) 1 (2,2)
- Page 250 and 251: 6 Sparse Matrices Now F = full(S) d
- Page 252 and 253: 6 Sparse Matrices Importing Sparse
- Page 254 and 255: 6 Sparse Matrices west0479 west0479
- Page 256 and 257: 6 Sparse Matrices The find Function
- Page 258 and 259: 6 Sparse Matrices of the rows and c
- Page 260 and 261: 6 Sparse Matrices The vertices of o
- Page 262 and 263: 6 Sparse Matrices 0 10 20 30 40 50
- Page 264 and 265: 6 Sparse Matrices 0 500 1000 1500 2
- Page 268 and 269: 6 Sparse Matrices Similarly, S(:,p)
- Page 270 and 271: 6 Sparse Matrices The following MAT
- Page 272 and 273: 6 Sparse Matrices 0 Original 0 Reve
- Page 274 and 275: 6 Sparse Matrices QR Factorization
- Page 276 and 277: 6 Sparse Matrices shows that A has
- Page 278 and 279: 6 Sparse Matrices Functions for Ite
- Page 280 and 281: 6 Sparse Matrices set up the five-p
- Page 282 and 283: 6 Sparse Matrices Manipulating Spar
- Page 284 and 285: 6 Sparse Matrices Selected Bibliogr
- Page 286 and 287: Index comparing sparse and full mat
- Page 288 and 289: Index H hb1dae demo 5-35 hb1ode dem
- Page 290 and 291: Index nonstiff ODE examples rigid b
- Page 292 and 293: Index LU factorization 6-30 minimum
- Page 294: Index twobvp demo 5-63 two-dimensio
Sparse Matrix Operations<br />
For example, the statements<br />
p = [1 3 4 2 5]<br />
I = eye(5,5);<br />
P = I(p,:);<br />
e = ones(4,1);<br />
S = diag(11:11:55) + diag(e,1) + diag(e,-1)<br />
produce<br />
p =<br />
P =<br />
S =<br />
1 3 4 2 5<br />
1 0 0 0 0<br />
0 0 1 0 0<br />
0 0 0 1 0<br />
0 1 0 0 0<br />
0 0 0 0 1<br />
11 1 0 0 0<br />
1 22 1 0 0<br />
0 1 33 1 0<br />
0 0 1 44 1<br />
0 0 0 1 55<br />
You can now try some permutations using the permutation vector p and the<br />
permutation matrix P. For example, the statements S(p,:) and P*S produce<br />
ans =<br />
11 1 0 0 0<br />
0 1 33 1 0<br />
0 0 1 44 1<br />
1 22 1 0 0<br />
0 0 0 1 55<br />
6-27