在Java Android中计算浮点数组距离的方法是什么?

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

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&lt;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&lt;diff.length;i++){
   diff+=arrayA[i]-arrayB[i];
}

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

发表评论

匿名网友

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

确定