Calculate Euclidean distance between 4-dimensional vectors

前端 未结 2 1491
广开言路
广开言路 2021-02-05 20:52

Let\'s say I have two 4-dimensional vectors (i.e. a and b) as follows:

a = {a1, a2, a3, a4}
b= {b1, b2, b3, b4}

How do I compute the Euclidean

2条回答
  •  耶瑟儿~
    2021-02-05 21:19

    public static float ndistance(float[] a, float[] b) {
        float total = 0, diff;
        for (int i = 0; i < a.length; i++) {
            diff = b[i] - a[i];
            total += diff * diff;
        }
        return (float) Math.sqrt(total);
    }
    

    The function/method/code above will calculate the distance in n-dimensional space. a and b are arrays of floating point number and have the same length/size or simply the n. Since you want a 4-dimension, you simply pass a 4-length array representing the data of your 4-D vector.

提交回复
热议问题