[Gretl-users] removing nan and inf from a matrix

Allin Cottrell cottrell at wfu.edu
Thu Jul 25 05:28:57 EDT 2013


On Tue, 23 Jul 2013, Logan Kelly wrote:

> I need to take the log difference of a matrix, i.e. log(M[2 
> rows(M):,]/M[1:rows(M)-1,]). Unfortunately, M has elements 
> equal to zero. I need to replace the nan's and inf's with 
> 0's. This almost works
>
> M = isnan(M) ? 0 : M
>
> but does not remove inf's. Any sugestions?

Is this calculation actually legit? Assuming it is, then

<hansl>
matrix M = muniform(15,2)
M[2,2] = 0
M[3,2] = 0
matrix ldM = log(M[2:rows(M),] - M[1:rows(M)-1,])
ldM = isnan(0 * ldM) ? 0 : ldM
print M ldM
</hansl>

This relies on the IEEE 754 rules: both 0*inf and 0*(-inf) 
return nan.

Allin Cottrell


More information about the Gretl-users mailing list