问题
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
...........
so It should be like:
r= 1.43, value=147 (70+77)
r= 1.42, value= 127 (63+64)
r= 1.41, value= 137 (70+67)
....
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);
//}
//}
}
}
UPDATE:
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);
}
}
}
This is what I get now:
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
回答1:
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! :)
来源:https://stackoverflow.com/questions/24047401/remove-duplicates-in-a-2d-array-and-add-the-corresponding-values