public int encode(double[] value, double confidence)
    {
        int winner=-1;

        // if out of boundary force to the limits
        for (int i=0;i<dimensionCount;i++)
        {
            if (value[i]<min[i])
                value[i]=min[i];
            if (value[i]>max[i])
                value[i]=max[i];
        }

        double mindis=1e15;
        // Loop through each unit
        for (int lc=0;lc<actualsize;lc++)
        {
            // Get the preferred value of the unit in each dimension
            double[] prefs=pref_value(decompressIX(lc));
            // Get squared euclidean distance between the encoded value and the unit's preferred values
            double diff=0;
            for (int i=0; i < dimensionCount; i++)
                diff+=(prefs[i]-value[i])*(prefs[i]-value[i]);
            // Pick the unit with the preferred value closest to the encoded value
            if (diff < mindis)
            {
                mindis=diff;
                winner=lc;
            }
        }

        if (winner==-1)
        {
            System.err.println("RCode.encode : ERROR cannot find winner !!! ");
        }

        int[] winnerIX=decompressIX(winner);
        /*
        // this is not very good because it encodes the value as a plus
        // instead of a ball around the best point
        int[] IX=decompressIX(winner);
        for (int k=0;k<dimensionCount;k++)
        {
            int ix=IX[k];
            for (int i=0; i<dimensionSize[k]; i++)
            {
                IX[k]=i;
                setFiringRate(IX,confidence[k] * Math.exp(-euclideanDistance(IX,winnerIX)/variance[k]));
            }
            setFiringRate(winnerIX,1.0);
        }
        */

        // this has to be improved by probably adding the covariance matrix in distance
        // metric. Currently it ignores the variance and makes perfect sphere around the
        // winner instead of a ellipse for example.

        // Loop through each unit
        for (int lc=0;lc<actualsize;lc++)
        {
            int[] IX=decompressIX(lc);
            // Compute the unit's firing rate as a gaussian centered around the winning unit times the confidence
            double act=confidence * Math.exp(-euclideanDistance(IX,winnerIX)/variance);
            if (act<0.00001)
                act=0;
            setFiringRate(lc,act);
        }
        return winner;
    }