任何人都可以为我提供如何找到我在下面的程序中得到的最终集群成员的中位数的解决方案.... ???
package javaapplication;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import java.io.BufferedInputStream; // to read text file line by line
public class microclustering{
private static boolean contains(String target, String value)
{
boolean result= true;
for(int i=0; i<value.length();i++){
if(target.indexOf(value.charAt(i))== -1)
{
result=false;
return result;
}
}
// System.out.println(count+"intersection "+a+b);
return result;
}
private static String[] arragedata(String [] data,int i, int j)
{
int length=data.length;
String newdata [] = new String[length-1];
if(i==j)
return data;
if(j<i)
{
int temp=j;
j=i;
i=temp;
}
int temp=0;
for(int k=0;k<data.length;k++)
{
if(k!=i && k!=j)
{
newdata[temp]=data[k];
temp++;
}
else
if(k==i)
{
newdata[temp]=data[i]+","+data[j];
temp++;
}
}
return newdata;
}
private static int getIntersection(String a, String b, String []database)
{
int count=0;
boolean flag=true;
String [] first = a.split(",");
String [] second = b.split(",");
for(int i=0; i<database.length;i++)
{
for(int j=0;j<first.length;j++)
if (!contains(database[i],first[j]))
flag=false;
if(flag==true)
for(int j=0;j<second.length;j++)
if (!contains(database[i],second[j]))
flag=false;
if(flag==true)
count++;
else
flag=true;
}
return count;
}
private static int getUnion(String a, String b, String []database)
{
int count=0;
boolean flag=false;
String [] first = a.split(",");
String [] second = b.split(",");
for(int i=0; i<database.length;i++)
{
for(int j=0;j<first.length;j++)
if (contains(database[i],first[j]))
flag=true;
if(flag==false)
for(int j=0;j<second.length;j++)
if (contains(database[i],second[j]))
flag=true;
if(flag==true)
{
count++;
flag=false;
}
}
return count;
}
public static void main(String[] args) throws Exception{
// TODO code application logic here
String [] database = {"abcefo", "acg", "ei", "acdeg", "acegl", "ej", "abcefp", "acd", "acegm", "acegn"};
String [] data = {"ecabf", "cad", "cag", "ecag", "ca", "eca", "e"};
String format1 = "###0.00" ;
DecimalFormat fm1 = new DecimalFormat( format1,new DecimalFormatSymbols(Locale.US));
boolean flag=true;
do{
double threshold=0.5; // set threshold = 0.5
int min_i=-1,min_j=-1;
float [][] output= new float[data.length][data.length];
for(int i=0;i<data.length;i++){
for(int j=0;j<data.length;j++)
{
if(getUnion(data[i],data[j],database)!= 0){
int b=getIntersection(data[i],data[j],database);
int c=getUnion(data[i],data[j],database);
float a=((float)b)/((float)c);
output[i][j]=1-a;
if(i==j)
output[i][j]=0;
}
}
} System.out.print(" \t");
for(int j=0;j<data.length;j++)
{
System.out.print(data[j]+"\t");
}
System.out.print("\n");
for(int i=0;i<data.length;i++)
{
System.out.print(data[i]+"\t");
for(int j=0;j<data.length;j++)
{
System.out.print(fm1.format(output[i][j])+"\t");
if(output[i][j]>0 && output[i][j]<=threshold)
{
threshold=output[i][j];
min_i=i;
min_j=j;
} }
System.out.print("\n");
}
if(min_i!=-1 && min_j!=-1)
{
System.out.println("minimum distance"+output[min_i][min_j]+"\t");
try{
data=arragedata(data,min_i,min_j);
}catch(Exception e){
e.printStackTrace();
flag=false;
}
}
else
flag=false;
System.out.print("\n\n\n");
}while(flag==true);
}
}
这是我的输出:
ecabf cad cag ecag ca eca e
ecabf 0.00 1.00 1.00 1.00 0.75 0.67 0.75
cad 1.00 0.00 0.83 0.80 0.75 0.86 0.89
cag 1.00 0.83 0.00 0.20 0.38 0.43 0.56
ecag 1.00 0.80 0.20 0.00 0.50 0.33 0.50
ca 0.75 0.75 0.38 0.50 0.00 0.25 0.40
eca 0.67 0.86 0.43 0.33 0.25 0.00 0.25
e 0.75 0.89 0.56 0.50 0.40 0.25 0.00
minimum distance0.19999999
ecabf cad cag,ecag ca eca e
ecabf 0.00 1.00 1.00 0.75 0.67 0.75
cad 1.00 0.00 0.83 0.75 0.86 0.89
cag,ecag 1.00 0.83 0.00 0.50 0.43 0.56
ca 0.75 0.75 0.50 0.00 0.25 0.40
eca 0.67 0.86 0.43 0.25 0.00 0.25
e 0.75 0.89 0.56 0.40 0.25 0.00
minimum distance0.25
ecabf cad cag,ecag ca eca,e
ecabf 0.00 1.00 1.00 0.75 0.75
cad 1.00 0.00 0.83 0.75 0.89
cag,ecag 1.00 0.83 0.00 0.50 0.56
ca 0.75 0.75 0.50 0.00 0.40
eca,e 0.75 0.89 0.56 0.40 0.00
minimum distance0.39999998
ecabf cad cag,ecag ca,eca,e
ecabf 0.00 1.00 1.00 0.80
cad 1.00 0.00 0.83 0.90
cag,ecag 1.00 0.83 0.00 0.60
ca,eca,e 0.80 0.90 0.60 0.00
我需要中位数的ca,eca,e和ecag,cag ...
答案 0 :(得分:1)
你的问题并不清楚。如果要计算中值,请使用此方法: -
如果这不是学校作业,那么您可以使用Commons Math来减少要编写的代码量。例如: -
DescriptiveStatistics ds = new DescriptiveStatistics();
ds.addValue(10);
ds.addValue(10);
ds.addValue(30);
// median is basically 50th percentile
System.out.println(ds.getPercentile(50)); // displays 10
答案 1 :(得分:-1)
要计算中位数,您可以使用例如问题"How to calculate the median of an array, Java?"的答案中给出的代码段。