Page 500 - Applied Numerical Methods Using MATLAB
P. 500

APPENDIX H















                                 SPARSE MATRICES









            A matrix is said to be sparse if it has a large portion of zero elements. MATLAB
            has some built-in functions/routines that enable us to exploit the sparsity of a
            matrix for computational efficiency.
              The MATLAB routine sparse() can be used to convert a (regular) matrix
            into a sparse form by squeezing out any zero elements and to generate a sparse
            matrix having the elements of a vector given together with the row/column index
            vectors. On the other hand, the MATLAB routine full() can be used to convert
            a matrix of sparse form into a regular one.
            >>row_index = [11234]; col_index = [12234]; elements = [1 2 3 4 5];
            >>m =4;n=4;As= sparse(row_index,col_index,elements,m,n)
               As = (1,1)      1
                   (1,2)       2
                   (2,2)       3
                   (3,3)       4
                   (4,4)       5
            >>Af = full(As)
              Af =  1    2    0    0
                    0    3    0    0
                    0    0    4    0
                    0    0    0    5
              We can use the MATLAB routine sprandn(m,n,nzd) to generate an m × n
            sparse matrix having the given non-zero density nzd. Let us see how efficient
            the operations can be on the matrices in sparse forms.

            >>As = sprandn(10,10,0.2); %a sparse matrix and
            >>Af = full(As); its full version

                                          
            Applied Numerical Methods Using MATLAB , by Yang, Cao, Chung, and Morris
            Copyright   2005  John  Wiley  &  Sons,  I nc., ISBN 0-471-69833-4


                                                                            489
   495   496   497   498   499   500   501   502   503   504   505