Averaging elements of a list of matrices over time

Quiet @ Block[{Indeterminate = 0}, Total[#] / Total[Unitize @ #]] & @ {t1, t2, t3}

% // MatrixForm // TeXForm

$\left( \begin{array}{ccc} 0 & 4 & 6 \\ 4 & 0 & \frac{9}{2} \\ 3 & 5 & 0 \\ \end{array} \right)$

Also

Total[{t1, t2, t3}] / (Total[Unitize @ {t1, t2, t3}] /. 0 -> 1)

same result


Total[{t1, t2, t3}]/Clip[Total[Unitize@{t1, t2, t3}], {1, Infinity}]

You can do it as a pipeline of functions, using MapThread; a properly placed Replace gets rid of the warnings:

MapThread[
 List /* DeleteCases[0] /* Replace[{} -> {0}] /* Mean,
 {t1, t2, t3}, 2]

(* {{0, 4, 6}, {4, 0, 9/2}, {3, 5, 0}} *)

Tags:

Matrix