Flatten matrix