Generic matrix implementation