所以目前我有以下内容通过从我的"距离"中获取最小距离值来找到最短/最近邻居。具有计算距离的数组。然后它进行另一次搜索以追踪它的索引,然后该索引向我指示它属于哪个病人。
但是说我想找到最近的3个邻居,我该怎么做?我是否需要完全更改我的代码才能适应这种情况?
非常感谢
int min = 99;
int d = 1;
String diagnosis;
//Finding smallest value from an array containing distance to new 'patient'
for(d=1; d<= numberOFinstances; d++){
if(distance[d] < min)
min = distance[d];
}
for (int p = 1; p < numberOFinstances; p++)
{
if (distance[p] == min){
System.out.println("Nearest patient to new patient is Patient "+p+ " with a distance of: " + min);
//Here I'm saying 6 because the diagnosis is in column 6 within the matrix
diagnosis = data[p][6];
System.out.println("The new patient's diagnosis is: " + diagnosis);
}
}
答案 0 :(得分:0)
最好的方法是使用Arrays.sort(int [])
Arrays.sort(distance);
int[] toReturn = new int[k];
for (int i = 0; i < k; i++) {
toReturn[i] = distance[i];
}