MATLAB Mathematics - SERC - Index of
MATLAB Mathematics - SERC - Index of MATLAB Mathematics - SERC - Index of
6 Sparse Matrices Manipulating Sparse Matrices Because sparse matrices are stored in a column-major format, accessing the matrix by columns is more efficient than by rows. Compare the time required for adding rows of a matrix 1000 times S = sparse(10000,10000,1); tic; for n = 1:1000 A = S(100,:) + S(200,:); end; toc Elapsed time is 1.208162 seconds. versus the time required for adding columns S = sparse(10000,10000,1); tic; for n = 1:1000 B = S(:,100) + S(:,200); end; toc Elapsed time is 0.088747 seconds. When possible, you can transpose the matrix, perform operations on the columns, and then retranspose the result: S = sparse(10000,10000,1); tic; for n = 1:1000 A = S(100,:)' + S(200,:)'; A = A'; end; toc Elapsed time is 0.597142 seconds. The time required to transpose the matrix is negligible. Note that the sparse matrix memory requirements may prevent you from transposing a sparse matrix having a large number of rows. This might occur even when the number of nonzero values is small. 6-42
Sparse Matrix Operations Using linear indexing to access or assign an element in a large sparse matrix will fail if the linear index exceeds intmax. To access an element whose linear index is greater than intmax, use array indexing: S = spalloc(216^2, 216^2, 2) S(1) = 1 S(end) = 1 S(216^2,216^2) = 1 6-43
- 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 266 and 267: 6 Sparse Matrices simply sparse(m,n
- 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 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
6 Sparse Matrices<br />
Manipulating Sparse Matrices<br />
Because sparse matrices are stored in a column-major format, accessing the<br />
matrix by columns is more efficient than by rows. Compare the time required<br />
for adding rows <strong>of</strong> a matrix 1000 times<br />
S = sparse(10000,10000,1);<br />
tic;<br />
for n = 1:1000<br />
A = S(100,:) + S(200,:);<br />
end;<br />
toc<br />
Elapsed time is 1.208162 seconds.<br />
versus the time required for adding columns<br />
S = sparse(10000,10000,1);<br />
tic;<br />
for n = 1:1000<br />
B = S(:,100) + S(:,200);<br />
end;<br />
toc<br />
Elapsed time is 0.088747 seconds.<br />
When possible, you can transpose the matrix, perform operations on the<br />
columns, and then retranspose the result:<br />
S = sparse(10000,10000,1);<br />
tic;<br />
for n = 1:1000<br />
A = S(100,:)' + S(200,:)';<br />
A = A';<br />
end;<br />
toc<br />
Elapsed time is 0.597142 seconds.<br />
The time required to transpose the matrix is negligible. Note that the sparse<br />
matrix memory requirements may prevent you from transposing a sparse<br />
matrix having a large number <strong>of</strong> rows. This might occur even when the number<br />
<strong>of</strong> nonzero values is small.<br />
6-42