edited cahematrix.R