May 2020
Intermediate to advanced
656 pages
16h 6m
English
A matrix is said to be sparse if it has a large portion of zero elements. MATLAB has some built‐in functions/routines, which enables us to exploit the sparsity of a matrix for computational efficiency.
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 function ‘
full()’ can be used to convert a matrix of sparse form into a regular one.>row_index=[1 1 2 3 4]; col_index=[1 2 2 3 4]; 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 00 3 0 00 0 4 00 0 0 5
We can use the MATLAB function ‘
sprandn(m,n,nzd)’ to generate an
m×
n sparse matrix having the given nonzero 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>AsA=As*As; % in sparse forms (50 flops)>AfA=Af*Af; % in full(regular) forms (2000 flops)>b=ones(10,1);>x=As\b; % in sparse forms (160 flops)>x=Af\b; % in full(regular) forms (592 flops)>inv(As); % in sparse forms (207 flops)>inv(Af); % in full(regular) forms (592 flops)>[L,U,P]=lu(As); % in sparse forms (53 flops)>f[L,U,P]=lu(Af); % in full(regular) forms (92 flops)
Read now
Unlock full access