首页 > 解决方案 > 给定计算的程序给出错误的输出

问题描述

任何人都可以帮助我检查为什么代码没有给我正确的答案或我做错了什么。我会感谢你的帮助。
此外,下面显示了我尝试实现的计算和代码。提前致谢。

扇形度量的计算如下所示

给定一个数据 Si = { 0.2554, 0.4464 , 0.3897 , 4.2112} ,例如迭代每个值

========================================================
Sj1 = 0.2554                                 Si1 = {0.2554}
Si1 > Sj1
0.2554 > 0.2554 = 0
Si1 = Sj2
0.2554 = 0.2554 = 1
Fand
(0   +   (0.5 * 1) )/ 1  =  0.5
==========================================================
Sj2 = 4.4464                                 Si2 = {0.2554, 4.4464}
Si2 > Sj2
0.2554 > 4.4464 = 0
4.4464 > 4.4464 = 0
Si2 = Sj2
0.2554 = 4.4464 = 0
4.4464 = 4.4464 = 1
Fand
( 0   +  (0.5 * 1) )/ 2  =  0.25
====================================================
Sj3 = 0.3897                               Si3 = {0.2554, 4.4464, 0.3897}
Si3 > Sj3
0.2554 > 0.3897 = 0
4.4464> 0.3897 = 1
0.3897 > 0.3897 = 0
Si3 = Sj3
0.2554 = 0.2554 = 0
4.4464 = 0.3897 = 0
0.3897 = 0.3897 = 1
Fand
( 1  +  (0.5 * 1) )/ 3  =  0.5  = 0.5
===========================================================
Sj4 = 4.2112                   Si4 = {0.2554, 0.4464, 0.3897, 0.42112}
Si4  > Sj4
0.2554 > 4.2112 = 0
0.4464 > 4.2112 = 1
0.3897 > 4.2112 = 0
0.42112 > 4.2112 = 0
Si4  = Sj4
0.2554 = 4.2112 = 0
0.4464 = 4.2112 = 0
0.3897 = 4.2112 = 0
0.42112 = 4.2112 = 1
Fand
( 1   +  (0.5 * 1) )/ 4  =  0.5 = 0.375
======================================================
Result
0.5, 0.25, 0.5, 0.375
The Fand formular in matlab code is
value = (sum( a(1:i) > a(i) ) + 0.5 * sum ( a(i) == a(1:i) ) ))/i
The Fand normal formular
Fand = ( #(si > sj) + 0.5 (si = sj)))/ i
======================================================

请在下面找到我的 Java 类 madion:

class madion {
    public static void main(String[] args) {
        double data[] = {0.2554, 4.4464, 0.3897, 4.2112};
        //initialise variables
        double sam = 0;
        double sam1 = 0;
        int i;
        int j = 0;
        double m = 0;
        double n = 0;
        int count = 0;
        int count1 = 0;
        //use for loop to iterate through the arrays
        for (i = 0; i < data.length; i++) {
            n = data[i];
            for (j = 0; j <= i; j++) {
                m = data[j];
                count = 0;
                //use if statement to express the conditions and counts
                if (n == m) {
                    count++;
                } else if (m > n) {
                    count1++;
                }
            }
            double fand = 0;
            for (i = 1; i <= data.length; i++) {
                // System.out.println(i );
                fand = ((count1) + (0.5 * count)) / i;
                System.out.println(fand);
                //  System.out.println(count);
                //   System.out.println(count1);
            }
        }
    }
}

我希望输出是,0.5, 0.25, 0.5, 0.375但我实际上得到了

0.5, 0.25, 0.1666, 0.125

标签: java

解决方案


Matlab 解决方案中存在三个不同的地方。

  1. 您除以的 i 在 Java 中从 0 开始,在 Matlab 中从 1 开始
  2. 那里有一个额外的循环来操纵 i 值
  3. 计数在不同级别上重置

代码应如下所示:

class madion {
    public static void main(String[] args) {
        double data[] = {0.2554, 4.4464, 0.3897, 4.2112};

        //use for loop to iterate through the arrays
        for (int i = 0; i < data.length; i++) {
            double n = data[i];
            int count = 0;
            int count1 = 0;
            for (int j = 0; j <= i; j++) {
                double m = data[j];
                //use if statement to express the conditions and counts
                if (n == m) {
                    count++;
                }
                if (m > n) {
                    count1++;
                }
            }
            double fand = ((count1) + (0.5 * count)) / (i+1);
            System.out.println(fand);
        }
    }
}

推荐阅读