add piece-wise mean for matrices