在Java中查找数组列表中的最小数。

huangapple go评论68阅读模式
英文:

Finding the smallest number in array list java

问题

以下是您要翻译的内容:

"So I have some code which is finding the distance between a series of points. One method uses the euclidean distance and is working fine, the other is using Manhattan and I don't know why it isn't working.

I have it set up so that the distance of the first element in the array list is zero for both methods, and therefore should print that image 1 is a match. However the Manhattan method always returns image 31, no matter how many different elements I test it with. I have double checked the elements in the array list and it should be returning image 1.

Does anybody have any ideas? Thanks in advance

public void matchEuclidean(){
for(int i = 0; i < numberimages; i++){
distanceE[i][0] = weights[i][0] - testweights[0][0];
distanceEi = weightsi - testweights0;
}

for(int i = 0; i &lt; numberimages; i++){
    distanceEu[i] = (Math.pow(distanceE[i][0], 2)) + (Math.pow(distanceE[i][1], 2));
    distanceEu[i] = Math.sqrt(distanceEu[i]);
   }  

for (double no : distanceEu) {
list.add(Double.valueOf(no));
}

double max= Collections.min(list);
double min = list.indexOf(max) + 1;
System.out.println("(euclidean) the unknown image matches image " + (min));

}

public void matchManhattan(){
for(int i = 0; i < numberimages; i++){
distanceM[i][0] = weights[i][0] - testweights[0][0];
distanceMi = weightsi - testweights0;
}

  for(int i = 0; i &lt; numberimages; i++){
    distanceMu[i] = distanceM[i][0] + distanceM[i][1];
    
   } 

 for (double no : distanceMu) {
 listM.add(Double.valueOf(no));
 }
  
 double max= Collections.min(listM);
 double min =  listM.indexOf(max) + 1;    
 System.out.println("(Manhattan) the unknown image matches image " + (min)); 

}"

英文:

So I have some code which is finding the distance between a series of points. One method uses the euclidean distance and is working fine, the other is using Manhattan and I don't know why it isn't working.

I have it set up so that the distance of the first element in the array list is zero for both methods, and therefore should print that image 1 is a match. However the Manhattan method always returns image 31, no matter how many different elements I test it with. I have double checked the elements in the array list and it should be returning image 1.

Does anybody have any ideas? Thanks in advance

public void matchEuclidean(){
    for(int i = 0; i &lt; numberimages; i++){
        distanceE[i][0] = weights[i][0] - testweights[0][0];
        distanceE[i][1] = weights[i][1] - testweights[0][1];
      }
      
    for(int i = 0; i &lt; numberimages; i++){
        distanceEu[i] = (Math.pow(distanceE[i][0], 2)) + (Math.pow(distanceE[i][1], 2));
        distanceEu[i] = Math.sqrt(distanceEu[i]);
       }  
       
     
for (double no : distanceEu) {
     list.add(Double.valueOf(no));
     }
      
   double max= Collections.min(list);
   double min =  list.indexOf(max) + 1;    
 System.out.println(&quot;(euclidean) the unknown image matches image &quot; + (min)); 


 }
 
 public void matchManhattan(){
     for(int i = 0; i &lt; numberimages; i++){
        distanceM[i][0] = weights[i][0] - testweights[0][0];
        distanceM[i][1] = weights[i][1] - testweights[0][1];
      }
      
      for(int i = 0; i &lt; numberimages; i++){
        distanceMu[i] = distanceM[i][0] + distanceM[i][1];
        
       } 
  
     for (double no : distanceMu) {
     listM.add(Double.valueOf(no));
     }
      
     double max= Collections.min(listM);
     double min =  listM.indexOf(max) + 1;    
     System.out.println(&quot;(Manhattan) the unknown image matches image &quot; + (min)); 


 }

答案1

得分: 1

看起来你忘记在曼哈顿距离中使用Math.abs函数了:

distanceMu[i] = Math.abs(distanceM[i][0]) + Math.abs(distanceM[i][1]);

没有它,你实际上没有一个有效的“距离”函数:你可能会得到负值,而且三角不等式不成立。

英文:

It looks like you neglected to use the Math.abs function in Manhattan distance:

distanceMu[i] = Math.abs(distanceM[i][0]) + Math.abs(distanceM[i][1]);

Without it, you don't really have a valid "distance" function: you can get negative values, and the triangle inequality does not hold

答案2

得分: 0

int a[] = {6, 22, 33, 12, 44, 9};

int low = a[0];

for (int i = 1; i < a.length; i++) {
    if (a[i] < low) {
        low = a[i];
    }
}

System.out.println("The smallest number in the given array is: " + low);
英文:
        int a[] = {6,22,33,12,44,9};
		
		int low = a[0];
		
		for(int i = 1 ; i &lt; a.length ; i ++ )

		{
			if(a[i] &lt; low)
			{
				low = a[i];
			}
			
		}
		System.out.println(&quot;The smallest number is the given array is : &quot; + low);
	}
}

huangapple
  • 本文由 发表于 2020年8月8日 21:44:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63316099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定