J
jimgardener
hi
i have a double[] ,the elements of which correspond to columns of a
double[][]. I need to get the double[][] sorted such that column
corresponding to largest element of double[] is first ,and column
corresponding to smallest element of double[] is last
for example if
double[] evalues=new double[]{2.1,3.2,1.5,7.4,5.2};
double[][] evectors=new double[][]{
{4.0,5.0,7.0,1.0,2.0},
{1.0,2.0,3.0,4.0,5.0},
{8.0,6.0,9.0,3.0,1.0},
{4.0,3.0,1.0,7.0,3.0},
{5.0,8.0,3.0,1.0,2.0}
};
after sorting i should get
evalues==>
7.4, 5.2 ,3.2, 2.1 ,1.5
and evectors==>
1.0, 2.0, 5.0, 4.0, 7.0
4.0, 5.0 ,2.0, 1.0, 3.0
3.0, 1.0, 6.0, 8.0, 9.0
7.0, 3.0, 3.0, 4.0, 1.0
1.0, 2.0, 8.0, 5.0, 3.0
I tried to write a function as below,but it looks too bulky and
superfluous..can someone suggest a more compact way
private void sortEigVectors(double[] eigValue,double[][]eigVector){
Hashtable<Double,double[]> table = new Hashtable<Double,double[]>
();
Double[] evals=new Double[eigValue.length];
for(int i=0;i<eigValue.length;i++){
evals=new Double(eigValue);
}
//put evals and evects into hashtable
for(int i=0;i<eigValue.length;i++){
Double key=evals;
double[] value=getVecOfCol(eigVector ,i);
table.put(key,value);
}
Enumeration<Double> keysen=table.keys();
ArrayList<Double> keylist=Collections.list(keysen);
//sort now Collections.sort(keylist,Collections.reverseOrder());//
largest first
for(int i=0;i<evals.length;i++){
double[] ret=table.get(keylist.get(i));//coumn i
setColumn(eigVector,ret,i);//update the double[][]
}
Double[] sortedkeys=new Double[keylist.size()];
keylist.toArray(sortedkeys);//store the sorted list elements in an
array
//use the array to update the original double[]eigValue
for(int i=0;i<sortedkeys.length;i++){
Double dbl=sortedkeys;
double dblval=dbl.doubleValue();
eigValue=dblval;
}
}
private static double[] getVecOfCol( double[][] mat, int j ){
int m = mat.length;
double[] res = new double[m];
for ( int i = 0; i < m; ++i ){
res = mat[j];
}
return(res);
}
private static void setColumn(double[][] mat,double[] col,int c){
int len=col.length;
for(int row=0;row<len;row++){
mat[row][c]=col[row];
}
}
thanks
jim
i have a double[] ,the elements of which correspond to columns of a
double[][]. I need to get the double[][] sorted such that column
corresponding to largest element of double[] is first ,and column
corresponding to smallest element of double[] is last
for example if
double[] evalues=new double[]{2.1,3.2,1.5,7.4,5.2};
double[][] evectors=new double[][]{
{4.0,5.0,7.0,1.0,2.0},
{1.0,2.0,3.0,4.0,5.0},
{8.0,6.0,9.0,3.0,1.0},
{4.0,3.0,1.0,7.0,3.0},
{5.0,8.0,3.0,1.0,2.0}
};
after sorting i should get
evalues==>
7.4, 5.2 ,3.2, 2.1 ,1.5
and evectors==>
1.0, 2.0, 5.0, 4.0, 7.0
4.0, 5.0 ,2.0, 1.0, 3.0
3.0, 1.0, 6.0, 8.0, 9.0
7.0, 3.0, 3.0, 4.0, 1.0
1.0, 2.0, 8.0, 5.0, 3.0
I tried to write a function as below,but it looks too bulky and
superfluous..can someone suggest a more compact way
private void sortEigVectors(double[] eigValue,double[][]eigVector){
Hashtable<Double,double[]> table = new Hashtable<Double,double[]>
();
Double[] evals=new Double[eigValue.length];
for(int i=0;i<eigValue.length;i++){
evals=new Double(eigValue);
}
//put evals and evects into hashtable
for(int i=0;i<eigValue.length;i++){
Double key=evals;
double[] value=getVecOfCol(eigVector ,i);
table.put(key,value);
}
Enumeration<Double> keysen=table.keys();
ArrayList<Double> keylist=Collections.list(keysen);
//sort now Collections.sort(keylist,Collections.reverseOrder());//
largest first
for(int i=0;i<evals.length;i++){
double[] ret=table.get(keylist.get(i));//coumn i
setColumn(eigVector,ret,i);//update the double[][]
}
Double[] sortedkeys=new Double[keylist.size()];
keylist.toArray(sortedkeys);//store the sorted list elements in an
array
//use the array to update the original double[]eigValue
for(int i=0;i<sortedkeys.length;i++){
Double dbl=sortedkeys;
double dblval=dbl.doubleValue();
eigValue=dblval;
}
}
private static double[] getVecOfCol( double[][] mat, int j ){
int m = mat.length;
double[] res = new double[m];
for ( int i = 0; i < m; ++i ){
res = mat[j];
}
return(res);
}
private static void setColumn(double[][] mat,double[] col,int c){
int len=col.length;
for(int row=0;row<len;row++){
mat[row][c]=col[row];
}
}
thanks
jim