Matrix class modified for 3d grid