A copy from Amp Lab mlmatrix for math 221 project