英文:
How to compute float array distance in java android
问题
在我的Android应用程序中,我有两个浮点数组,我想要计算它们之间的距离:
float[] arrayA;
float[] arrayB;
diff = ?
在Python中,我这样做:
diff = np.sum(abs(arrayA - arrayB))
我找不到Java中相同的运算符。
我应该使用for循环吗?或者有更快的方法吗?
英文:
Insime my Android app, I have two float arrays and I want to compute the distance:
float[] arrayA;
float[] arrayB;
diff = ?
In Python I do:
diff = np.sum(abs(arrayA - arrayB))
I can't find the same operators in java.
Should I use a for loop? Or there are faster ways?
答案1
得分: 1
尝试这段代码,如果你想对每个元素都进行操作:
float[] diff = new float[arrayA.length];//假设长度相同
for (int i = 0; i < diff.length; i++) {
diff[i] = arrayA[i] - arrayB[i];
}
或者,如果你只想要一个变量作为结果:
float diff = 0;
for (int i = 0; i < diff.length; i++) {
diff += arrayA[i] - arrayB[i];
}
英文:
Try this code if you want to have it for each element:
float[] diff=new float[arrayA.length];//Assuming same length
for(int i=0;i<diff.length;i++){
diff[i]=arrayA[i]-arrayB[i];
}
Or, if you only want one variable as result:
float[] diff=0;
for(int i=0;i<diff.length;i++){
diff+=arrayA[i]-arrayB[i];
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论