Index: trunk/MagicSoft/Mars/mhist/MHMatrix.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHMatrix.cc	(revision 1587)
+++ trunk/MagicSoft/Mars/mhist/MHMatrix.cc	(revision 1593)
@@ -427,5 +427,20 @@
     TMath::Sort(dists.GetSize(), dists.GetArray(), idx.GetArray(), kFALSE);
 
-    const Int_t n = TMath::Abs(num)<rows ? TMath::Abs(num) : rows;
+    Int_t from = 0;
+    Int_t to   = TMath::Abs(num)<rows ? TMath::Abs(num) : rows;
+    //
+    // This is a zero-suppression for the case a test- and trainings
+    // sample is identical. This would result in an unwanted leading
+    // zero in the array. To suppress also numerical uncertanties of
+    // zero we cut at 1e-5. Due to Rudy this should be enough. If
+    // you encounter problems we can also use (eg) 1e-25
+    //
+    if (dists[idx[0]]<1e-5)
+    {
+        from++;
+        to ++;
+        if (to>rows)
+            to = rows;
+    }
 
     if (num<0)
@@ -438,8 +453,8 @@
 
         Double_t res = 0;
-        for (int i=0; i<n; i++)
+        for (int i=from; i<to; i++)
             res += TMath::Exp(-dists[idx[i]]/hwin);
 
-        return -TMath::Log(res/n);
+        return -TMath::Log(res/(to-from));
     }
     else
@@ -449,8 +464,8 @@
         //
         Double_t res = 0;
-        for (int i=0; i<n; i++)
+        for (int i=from; i<to; i++)
             res += dists[idx[i]];
 
-        return TMath::Sqrt(res/n);
+        return TMath::Sqrt(res/(to-from));
     }
 }
