% Most matrices occurring in practice % have special structure. % 1. Sparse Matrices % MATLAB has facilities for handling sparse % matrices, matrices with few nonzero elements. a = sparse(4,6,3,10,10); % we defined a 10-by-10 matrix with one % nonzero element a(4,6) = 3 a a = (4,6) 3 spy(a) % provides a picture % Sierpinski gasket p32 = pascal(32); s32 = rem(p32,2); % remove the even entries spy(s32) % patterns shows the Sierpinski gasket 32*32 ans = 1024 p64 = pascal(64); s64 = rem(p64,2); % remove the even entries spy(s64) % patterns shows the Sierpinski gasket % % It is interested to see the "hole" in the gasket, % due to the exponentional growth in the numbers, % MATLAB is no longer able to determine the parity correctly. % help sparfun Sparse matrices. Elementary sparse matrices. speye - Sparse identity matrix. sprand - Sparse uniformly distributed random matrix. sprandn - Sparse normally distributed random matrix. sprandsym - Sparse random symmetric matrix. spdiags - Sparse matrix formed from diagonals. Full to sparse conversion. sparse - Create sparse matrix. full - Convert sparse matrix to full matrix. find - Find indices of nonzero elements. spconvert - Import from sparse matrix external format. Working with sparse matrices. nnz - Number of nonzero matrix elements. nonzeros - Nonzero matrix elements. nzmax - Amount of storage allocated for nonzero matrix elements. spones - Replace nonzero sparse matrix elements with ones. spalloc - Allocate space for sparse matrix. issparse - True for sparse matrix. spfun - Apply function to nonzero matrix elements. spy - Visualize sparsity pattern. Reordering algorithms. colamd - Column approximate minimum degree permutation. symamd - Symmetric approximate minimum degree permutation. colmmd - Column minimum degree permutation. symmmd - Symmetric minimum degree permutation. symrcm - Symmetric reverse Cuthill-McKee permutation. colperm - Column permutation. randperm - Random permutation. dmperm - Dulmage-Mendelsohn permutation. Linear algebra. eigs - A few eigenvalues, using ARPACK. svds - A few singular values, using eigs. luinc - Incomplete LU factorization. cholinc - Incomplete Cholesky factorization. normest - Estimate the matrix 2-norm. condest - 1-norm condition number estimate. sprank - Structural rank. Linear Equations (iterative methods). pcg - Preconditioned Conjugate Gradients Method. bicg - BiConjugate Gradients Method. bicgstab - BiConjugate Gradients Stabilized Method. cgs - Conjugate Gradients Squared Method. gmres - Generalized Minimum Residual Method. lsqr - Conjugate Gradients on the Normal Equations. minres - Minimum Residual Method. qmr - Quasi-Minimal Residual Method. symmlq - Symmetric LQ Method. Operations on graphs (trees). treelayout - Lay out tree or forest. treeplot - Plot picture of tree. etree - Elimination tree. etreeplot - Plot elimination tree. gplot - Plot graph, as in "graph theory". Miscellaneous. symbfact - Symbolic factorization analysis. spparms - Set parameters for sparse matrix routines. spaugment - Form least squares augmented system. % Let us convert a sparse matrix into dense format s8 = rem(pascal(8),2); size(8,8); size(s8) ans = 8 8 s8a = sparse(s8) s8a = (1,1) 1 (2,1) 1 (3,1) 1 (4,1) 1 (5,1) 1 (6,1) 1 (7,1) 1 (8,1) 1 (1,2) 1 (3,2) 1 (5,2) 1 (7,2) 1 (1,3) 1 (2,3) 1 (5,3) 1 (6,3) 1 (1,4) 1 (5,4) 1 (1,5) 1 (2,5) 1 (3,5) 1 (4,5) 1 (1,6) 1 (3,6) 1 (1,7) 1 (2,7) 1 (1,8) 1 [i,j,s] = find(s8) i = 1 2 3 4 5 6 7 8 1 3 5 7 1 2 5 6 1 5 1 2 3 4 1 3 1 2 1 j = 1 1 1 1 1 1 1 1 2 2 2 2 3 3 3 3 4 4 5 5 5 5 6 6 7 7 8 s = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 % in the triplet (i,j,s) we find the row % indices in i, column indices in j, and % the values at (i,j) in s % These triplets are useful to define sparse % matrices: sp8b = sparse(i,j,s); spy(sp8b); % to define a 50-by-50 tridiagonal matrix, % with 2 on the diagonal, and ones on the % lower and upper diagonal i = [ 1:50 2:50 1:49 ]; j = [ 1:50 1:49 2:50 ]; s = [ 2*ones(1,50) ones(1,49) ones(1,49) ]; d3 = sparse(i,j,s); spy(d3) d3(13,20:30) ans = All zero sparse: 1-by-11 d3(25,20:30) ans = (1,5) 1 (1,6) 2 (1,7) 1 % 8.2 is another application % 8.3 Multi-Dimensional matrices r = -2:0.1:2; [x,y,z] = ndgrid(r,r,r); t = y.*exp(-x.^2 - y.^2 - z.^2); slice(y,x,z,t,[],[-1.2 .8],-2); diary off