jeudi 1 janvier 2015

Remove duplicate row from 2D array


Vote count:

0




I have a two dimention integer array with 1000 rows and I need to remove dublicate rows from this array. the elements of this array have a value of zero or one. This is what I did so far: 1. first I tried to collect the each row elements into one element by applying the following code



for(int i=0;i<PosPop;i++){
Encoding[i]=0;
for(int j=0;j<20;j++){
Encoding[i]=Encoding[i]+IncProb1[i][j]*Math.pow(10, -(j+1));
}
}


2. After that I used Linkedhash array to remove dublicate as follow:



Set<Double> Encodingfltr =new LinkedHashSet<Double>();
for(double x : Encoding) {
Encodingfltr.add(x);
}
Double[] fltrdencodng = new Double[Encodingfltr.size()];
fltrdencodng = Encodingfltr.toArray(fltrdencodng);


3. finally I tried to construct the first matrix as follow:



int b=fltrdencodng.length;
double[][] PossIntr=new double[b][20];
for(int i=0;i<b;i++){
for(int j=0;j<20;j++){
Encoding[i]=Encoding[i]*Math.pow(10,j);
DecimalFormat newFormat = new DecimalFormat("#");
double integern = Double.valueOf(newFormat.format(Encoding[i]));
DecimalFormat newFormat1 = new DecimalFormat("#.#");
double OneDecmal = Double.valueOf(newFormat1.format(Encoding[i]));
PossIntr[i][j]= (Math.round((OneDecmal-integern)*10));
}
}


But I have many problem applying this procedure: first in the first step I get some rounding error for example if a row in matrix IncProb1[i][j] is 0 0 1 1 1 1 1 1 1 0 1 1 0 1 0 0 0 0 0 0 then applying the first step I would get this number 0.011111110110099999 instead of this 0.0111111101101000000 Also the third step doesn't work I get the following matrix element 0.0 0.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0


which is very different from the original


Is there better method to do this Any help is highly appreciated


Thanks in advance.



asked 37 secs ago







Remove duplicate row from 2D array

Aucun commentaire:

Enregistrer un commentaire