How to compute float array distance in java android

Viewed 76

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 Answers

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];
}
Related