If no one told you that M was a matrix you would write T = aeMt as the solution where a is the value of T when t=0. It turns out that the equation works even if M is a matrix and T is a vector whose components are the temperatures if you write
T = eMta
and it turns out that eMt can be evaluated by the Taylor series just as if Mt were a real number.

eM = Σj≥0Mj/j!

eMt is a square matrix the same size as M.

Some code that agrees with the naïve code. (Routine expm exponentiates a matrix.)

If x is negative the Taylor series for ex is expensive. If exp(x) directly applies the Taylor series then 1.0/exp(-x) is faster than exp(x) for negative x. Similarly if you suspect that most of the eigenvalues are negative inv(expm(−M)) is cheaper than expm(x) to compute.

This hack avoids limitations on M such as symmetry or even lop-sidedness. M can even be complex.