Added code to calculate sparse matrix.