Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的迭代求和_Java_Arrays_Algorithm_Sum - Fatal编程技术网

java中的迭代求和

java中的迭代求和,java,arrays,algorithm,sum,Java,Arrays,Algorithm,Sum,我在用Java开发此算法时遇到了麻烦,请遵循以下步骤: 我有一个double数组和一个变量double 我需要开发一个算法,将数组的值和一个变量求和——这一部分是可以的 问题是:我需要将这个和迭代x次(例如2次、3次……或80次),变量和的值与数组的所有值一起迭代,它将其值“扩散”到数组中的值,例如: 1.0 - double variable 0.1 - 1st value in array 0.5 - 2nd value in array 0.8 - 3rd value in array

我在用Java开发此算法时遇到了麻烦,请遵循以下步骤:

我有一个double数组和一个变量double

我需要开发一个算法,将数组的值和一个变量求和——这一部分是可以的

问题是:我需要将这个和迭代x次(例如2次、3次……或80次),变量和的值与数组的所有值一起迭代,它将其值“扩散”到数组中的值,例如:

1.0 - double variable
0.1 - 1st value in array
0.5 - 2nd value in array
0.8 - 3rd value in array
在第一次迭代中,我们有-不要对0.333!感到厌烦

所以,我们必须用双变量求和所有结果,然后

0.433 + 0.833 + 1.1333 + 1 = 2.4
在下一次迭代中,公式中将替换2.4值和所有其他值

formule = 0.433 + (2.4 * 0.3333) = 1.2325
formule = 0.833 + (2.4 * 0.333) = 1.63325
formule = 1.133 + (2.4 * 0.333) = 1.932
因此,我们必须用新的双变量(2.4)求和所有结果,然后

然后,重复这个过程,直到迭代完成

该算法基于Melnik提出的相似泛洪算法

遵循我的算法

 double [] sfAttributes = new double [3]; 

    double [] sfNewAttributes = new double [3]; 

    double [] simLinksAttributes = {0.1, 0.5, 0.8}; // array with values

    double sfClass = 0.0d;

    double simLinkClass = 1.0d; // the double variable!

    int iter = 3; // amout of iterations

    // set the first iteration value
    for (int i = 0; i < similaridadeLinksAtributos.length; i++){


        sfAttributes[i] = simLinkAttributes[i] + (simLinkClass * 0.333);

        // here, sum all values of the simLinkAttributes[i]
        // and put the results in sfClass variable
        sfClass +=  similaridadeLinksAtributos[i];

    }

    sfClasses = (sfClasses + simLinkClass);

 // here starts the iterations.

    for (int i = 0; i < iter; i++){

        for (int j = 0; j < simLinkAttributes.length; j++){


            sfNewAttributes[j] = sfAttributes[j] + (sfClass * 0.333);

            sfClass +=  sfNewAttributes[j];


    }

}
试试这个

    double[] sfAttributes = new double[3];
    double[] simLinksAttributes = {0.1, 0.5, 0.8}; // array with values
    double sfClass = 0.0d;
    double simLinkClass = 1.0d; // the double variable!
    int iter = 3; // amout of iterations

    // set the first iteration value
    for (int i = 0; i < simLinksAttributes.length; i++) {
        sfAttributes[i] = simLinksAttributes[i] + (simLinkClass * 0.333);
        // here, sum all values of the simLinkAttributes[i]
        // and put the results in sfClass variable
        sfClass += simLinksAttributes[i];
    }
    double[] sfNewAttributes = sfAttributes;

    sfClass = (sfClass + simLinkClass);
    System.out.println(sfClass);
    // here starts the iterations.

    for (int i = 0; i < iter; i++) {
        for (int j = 0; j < simLinksAttributes.length; j++) {
            sfNewAttributes[j] = sfNewAttributes[j] + (sfClass * 0.333);
        }

        for (int k =0 ; k < sfNewAttributes.length; k++) {
            sfClass += sfNewAttributes[k];
        }
        System.out.println(sfClass);
    }
double[]sfAttributes=新的双精度[3];
双[]simLinksAttributes={0.1,0.5,0.8};//带值数组
双sfClass=0.0d;
双simLinkClass=1.0d;//双重变量!
int iter=3;//迭代次数
//设置第一个迭代值
对于(int i=0;i
您现在面临哪些问题?您的实际结果与预期结果有何差异?1.2325+1.63325+1.932+2.4=4.799????
 double [] sfAttributes = new double [3]; 

    double [] sfNewAttributes = new double [3]; 

    double [] simLinksAttributes = {0.1, 0.5, 0.8}; // array with values

    double sfClass = 0.0d;

    double simLinkClass = 1.0d; // the double variable!

    int iter = 3; // amout of iterations

    // set the first iteration value
    for (int i = 0; i < similaridadeLinksAtributos.length; i++){


        sfAttributes[i] = simLinkAttributes[i] + (simLinkClass * 0.333);

        // here, sum all values of the simLinkAttributes[i]
        // and put the results in sfClass variable
        sfClass +=  similaridadeLinksAtributos[i];

    }

    sfClasses = (sfClasses + simLinkClass);

 // here starts the iterations.

    for (int i = 0; i < iter; i++){

        for (int j = 0; j < simLinkAttributes.length; j++){


            sfNewAttributes[j] = sfAttributes[j] + (sfClass * 0.333);

            sfClass +=  sfNewAttributes[j];


    }

}
/* Results
         * 
         *       0 Interaction
         * 2,4 - simLinkClass variable
         * 0,433 - 1st new value of array
         * 0,833 - 2nd new value of array
         * 1,133 - 3rd new value of array
         * 
         *      1st Interaction
         * 4,799 - simLinkClass variable
         * 1,2325 - 1st new value of array
         * 1,63325 - 2nd new value of array
         * 1,932 - 3rd new value of array
         * 
         *      2nd Iteraction
         * 9,58675 - simLinkClass variable
         * 2,830567 - 1st new value of array
         * 2,3227562 -  2nd new value of array
         * 3,530067 -  3rd new value of array
         * 
         *      
         * 
         */
    double[] sfAttributes = new double[3];
    double[] simLinksAttributes = {0.1, 0.5, 0.8}; // array with values
    double sfClass = 0.0d;
    double simLinkClass = 1.0d; // the double variable!
    int iter = 3; // amout of iterations

    // set the first iteration value
    for (int i = 0; i < simLinksAttributes.length; i++) {
        sfAttributes[i] = simLinksAttributes[i] + (simLinkClass * 0.333);
        // here, sum all values of the simLinkAttributes[i]
        // and put the results in sfClass variable
        sfClass += simLinksAttributes[i];
    }
    double[] sfNewAttributes = sfAttributes;

    sfClass = (sfClass + simLinkClass);
    System.out.println(sfClass);
    // here starts the iterations.

    for (int i = 0; i < iter; i++) {
        for (int j = 0; j < simLinksAttributes.length; j++) {
            sfNewAttributes[j] = sfNewAttributes[j] + (sfClass * 0.333);
        }

        for (int k =0 ; k < sfNewAttributes.length; k++) {
            sfClass += sfNewAttributes[k];
        }
        System.out.println(sfClass);
    }