I guess you have a problem in the forward model.
To test correctly the Gain matrix for head model, you cannot use "==nan" because "NaN==NaN" returns 0 in Matlab, these are not numbers and are not equal to anything.
Instead, use:
nnz(isnan(HeadmodelMat.Gain))
nnz(isinf(HeadmodelMat.Gain))