Function to calculate profile of sparse matrix