replace padarray by matrix stacking