In: Advanced Math
23. Consider the 5 x 6 matrix A whose (i,j)h element is aij i+j;
(a) What is A(10:20)?
(b) What is k such that A(k) = A(3,4)?
(c) Show how A is stored in sparse column format
(d) Show how A is stored in sparse row format
(e) What is the sparsity ratio?
Matlab code
A=zeros(5,6);
for i=1:5
for j=1:6
A(i,j)=i+j;
end
end
A
A(10:20)%(a)
A(3,4)
A(10)%k=10 (b)
S=sparse(A)%(c)sparse column format
S'%(d)Sparse row format
sparsity_ratio=1-nnz(A)/numel(A)%(e)1-The density of a matrix is
the ratio of nonzeros to the total number of elements
Output
A =
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
5 6 7 8 9 10
6 7 8 9 10 11
ans =
7 4 5 6 7 8 5 6 7 8 9
ans =
7
ans =
7
S =
(1,1) 2
(2,1) 3
(3,1) 4
(4,1) 5
(5,1) 6
(1,2) 3
(2,2) 4
(3,2) 5
(4,2) 6
(5,2) 7
(1,3) 4
(2,3) 5
(3,3) 6
(4,3) 7
(5,3) 8
(1,4) 5
(2,4) 6
(3,4) 7
(4,4) 8
(5,4) 9
(1,5) 6
(2,5) 7
(3,5) 8
(4,5) 9
(5,5) 10
(1,6) 7
(2,6) 8
(3,6) 9
(4,6) 10
(5,6) 11
ans =
(1,1) 2
(2,1) 3
(3,1) 4
(4,1) 5
(5,1) 6
(6,1) 7
(1,2) 3
(2,2) 4
(3,2) 5
(4,2) 6
(5,2) 7
(6,2) 8
(1,3) 4
(2,3) 5
(3,3) 6
(4,3) 7
(5,3) 8
(6,3) 9
(1,4) 5
(2,4) 6
(3,4) 7
(4,4) 8
(5,4) 9
(6,4) 10
(1,5) 6
(2,5) 7
(3,5) 8
(4,5) 9
(5,5) 10
(6,5) 11
sparsity_ratio =
0