in reply to Calculate log of matrix values

Computationally hazardous(!), but you could try one of these modifications with e.g. 1e-300 as an approximation for zero and see how far you get... Take care of error propagation.

$lnpam[$i][$j] = log( $matrix[$i][$j] || 1e-300);

$lnpam[$i][$j] = $matrix[$i][$j] ? log( $matrix[$i][$j]) : -1000;