【问题标题】:Remove duplicates in a 2D array and add the corresponding values删除二维数组中的重复项并添加相应的值
【发布时间】:2014-06-05 05:13:34
【问题描述】:

我知道有很多关于删除重复项,但我似乎无法正确处理,我想知道是否有人能告诉我我做错了什么。因此,下面的代码包含一个嵌套的 for 循环,它遍历图像(由大小为 256x256 的矩阵组成),然后将其传递给 ImagePlus 以计算半径、θ 和值。问题是半径中有重复项,我想为每个重复项总结如下值:

......
r=1.44 mm/c (167), value=63   
r=1.43 mm/c (167), value=77   
r=1.43 mm/c (168), value=70   
r=1.42 mm/c (169), value=63   
r=1.42 mm/c (169), value=64   
r=1.41 mm/c (170), value=70   
r=1.41 mm/c (171), value=67   
r=1.40 mm/c (171), value=71 
........... 

所以应该是这样的:

r= 1.43, value=147 (70+77)

r= 1.42, value= 127 (63+64)

r= 1.41, value= 137 (70+67)
....

这是我一直在尝试的,但我没有任何运气!我也尝试过使用 Sets,但我需要它按特定顺序排列,它不能把它搞砸。

 final XYSeries data = new XYSeries("Third");
 double rMax = -1;
double [][] radiusArray = new double[256][256];
double [][] valueArray = new double[256][256];
 for(int i =0; i< 256; i++){
  for(int y =0; y< 256; y++){
     //image.getPixel(i, y);
       //This is taking the pixel position and calculating the r and value at that pixel
     String x = image.getLocationAsString(i, y);
    String n = image.getValueAsString(i, y);


    String delim = ", value=";
    String [] tokens = n.split(delim);
    double num = Double.parseDouble(tokens[1]);

    //if(image.getR() < 1.43){
    String [] t = x.split("r=");
    String[] b = t[1].split(" mm/c");
    //System.out.print("Meet b:    "+b[0]);
    double radius = Double.parseDouble(b[0]);

    String [] theta = x.split("theta= ");
    String [] token2 = theta[1].split(Character.toString(IJ.degreeSymbol));
    float thetaNum = Float.parseFloat(token2[0]);
    //System.out.print("  This is the theta value:    "+thetaNum+"    ");

    if(radius > rMax){
       rMax = radius;
    }

    radiusArray[i][y] = radius;
    valueArray[i][y] = num;     

    //if(thetaNum <= 180.00){
    System.out.print(x);
    System.out.print(n);
    System.out.print("   "+num);

    System.out.println();
    data.add(radius, num);

    //}
//}
}
}

更新:

所以我能够摆脱重复,但现在它似乎每隔一个数字就跳过一次,我不知道为什么?

    double summation;
         for(int i=1; i< 256; i++){
             for(int y=1; y< 256; y++){
                 if(radiusArray[i] != radiusArray[y]){
                     //System.out.print("its okay"+radiusArray[i][y]+"     ");
                     String n = image.getValueAsString(i, y);

                     //System.out.println(valueArray[i][y]);
                     String delim = ", value=";
                        String [] tokens = n.split(delim);
                        double num = Double.parseDouble(tokens[1]);
   //                           System.out.print(radiusArray[i][y]);
  //                            System.out.println("    value=    "+num);
                 }
                 else{
                     String n = image.getValueAsString(i, y);
                     String m = image.getValueAsString(i-1, y-1);
                        String delim = ", value=";
                        String [] tokens = n.split(delim);
                        double num = Double.parseDouble(tokens[1]);

                        String mDelim = ", value=";
                        String [] mtokens = m.split(delim);
                        double mnum = Double.parseDouble(tokens[1]);
                        summation = mnum+ num;

                        System.out.print(radiusArray[i][y]);
                        System.out.println("   value=   "+summation);
                 }

             }
         }

这是我现在得到的:

1.64   value=   186.0
1.62   value=   130.0
1.61   value=   120.0
1.59   value=   150.0
1.58   value=   134.0
1.56   value=   130.0
1.55   value=   136.0
1.54   value=   108.0
1.52   value=   144.0
1.51   value=   118.0

【问题讨论】:

    标签: java duplicates


    【解决方案1】:

    试试这个来找到骗子,注意不要在循环时删除;这会导致数组越界。也许再制作一个array 标记为 duplicate 来添加重复项,并在找到它们后将其删除。

    for(int i = 0; i < array.length; i++) {
        for(int j = 0; j < array.length; j++) {
            if(array[i] == array[j]) {
               // "i" would be a duplicate...
               break;
            }
        }
    }
    

    这样您就不会丢失数组的顺序。只需确保您找到重复项,然后将其删除即可。

    希望这会有所帮助! :)

    【讨论】:

    • 好的,我试过了,但现在它每隔一个数字跳过 r。您现在可以在我更新的问题中看到它。好吧,基本上它错过了没有重复的数字,如果我把 if 语句 - print 语句放回去,那么我会再次在代码中看到重复项。我不知道如何摆脱这个。
    猜你喜欢
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 2017-05-07
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    相关资源
    最近更新 更多