删除2D数组中的重复项并添加相应的值 [英] Remove duplicates in a 2D array and add the corresponding values

查看:98
本文介绍了删除2D数组中的重复项并添加相应的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know theres a lot about removing duplicates but I can't seem to get it right, I was wondering if anyone could tell me what I am doing wrong. So the code below contains a nested for loop where it traverses through a image (that is a made of a matrix of size 256x256) then it is passed to ImagePlus to calculate the radius, theta and value. The problem is that in radius there are duplicates and I want to for every duplicate to sum up the value like this:

......
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,但是我需要按照特定的顺序进行操作,并且不能弄乱它.

This is what I have been trying but I haven't had any luck! I have also tried using Sets but I need this to be in a specific order and it can't mess that up.

 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);

    //}
//}
}
}

更新:

所以我能够消除重复项,但是现在好像每隔一个数字就会跳过一次,我不知道为什么吗?

So I was able to get rid of duplicates but now it seems to be skipping every second number and I don't know why?

    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

推荐答案

尝试使用此方法查找欺骗,请注意尽管循环时不要删除;这将导致数组超出范围.也许再制作一个标记为 duplicate array来添加重复对象,并在找到它们后将其删除.

Try this to find dupe, note though do not remove while looping; that would cause an array out of bounds. Maybe make another array labeled duplicate to add the dupes too and remove them after you find them.

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;
        }
    }
}

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

This way you wouldn't lose the order of your arrays. Just make sure you find the duplicates then remove them.

希望这会有所帮助! :)

Hope this helps! :)

这篇关于删除2D数组中的重复项并添加相应的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆