假设有两个包含了三维空间点坐标的,三维向量集 A A A 和 B B B,两集合中分别有 m m m 个和 n n n 个三维空间坐标点,可以用矩阵表示为
A = [ a 1 x a 2 x a 3 x ⋯ a m x a 1 y a 2 y a 3 y ⋯ a m y a 1 z a 2 z a 3 z ⋯ a m z ] 3 × m , B = [ b 1 x b 2 x b 3 x ⋯ b n x b 1 y b 2 y b 3 y ⋯ b n y b 1 z b 2 z b 3 z ⋯ b n z ] 3 × n A = \left[\begin{matrix} a_1^x & a_2^x & a_3^x & \cdots & a_m^x \\ a_1^y & a_2^y & a_3^y & \cdots & a_m^y \\ a_1^z & a_2^z & a_3^z & \cdots & a_m^z \\ \end{matrix}\right]_{3 \times m}, \quad B = \left[\begin{matrix} b_1^x & b_2^x & b_3^x & \cdots & b_n^x \\ b_1^y & b_2^y & b_3^y & \cdots & b_n^y \\ b_1^z & b_2^z & b_3^z & \cdots & b_n^z \\ \end{matrix}\right]_{3 \times n} A= a1xa1ya1za2xa2ya2za3xa3ya3z⋯⋯⋯amxamyamz 3×m,B= b1xb1yb1zb2xb2yb2zb3xb3yb3z⋯⋯⋯bnxbnybnz 3×n
或
A = [ a 1 x a 1 y a 1 z a 2 x a 2 y a 2 z a 3 x a 3 y a 3 z ⋮ ⋮ ⋮ a m x a m y a m z ] m × 3 , B = [ b 1 x b 1 y b 1 z b 2 x b 2 y b 2 z b 3 x b 3 y b 3 z ⋮ ⋮ ⋮ b n x b n y b n z ] n × 3 A = \left[\begin{matrix} a_1^x & a_1^y & a_1^z \\ a_2^x & a_2^y & a_2^z \\ a_3^x & a_3^y & a_3^z \\ \vdots & \vdots & \vdots \\ a_{m}^x & a_{m}^y & a_{m}^z \\ \end{matrix}\right]_{m \times 3}, \quad B = \left[\begin{matrix} b_1^x & b_1^y & b_1^z \\ b_2^x & b_2^y & b_2^z \\ b_3^x & b_3^y & b_3^z \\ \vdots & \vdots & \vdots \\ b_{n}^x & b_{n}^y & b_{n}^z \\ \end{matrix}\right]_{n \times 3} A= a1xa2xa3x⋮amxa1ya2ya3y⋮amya1za2za3z⋮amz m×3,B= b1xb2xb3x⋮bnxb1yb2yb3y⋮bnyb1zb2zb3z⋮bnz n×3
A
A
A 集合中三维坐标点
a
i
a_i
ai 与
B
B
B 集合中点
b
j
b_j
bj 的距离为
d
i
j
=
(
a
i
x
−
b
j
x
)
2
+
(
a
i
y
−
b
j
y
)
2
+
(
a
i
z
−
b
j
z
)
2
d_{ij} = \sqrt{ (\red{a_i^x}-\blue{b_j^x})^2 + (\red{a_i^y}-\blue{b_j^y})^2 + (\red{a_i^z}-\blue{b_j^z})^2 }
dij=(aix−bjx)2+(aiy−bjy)2+(aiz−bjz)2
分析上式,可以得到以下计算思路
( a i x − b j x ) 2 + ( a i y − b j y ) 2 + ( a i z − b j z ) 2 = ( a i x ) 2 + ( b j x ) 2 − 2 a i x b j x + ( a i y ) 2 + ( b j y ) 2 − 2 a i y b j y + ( a i z ) 2 + ( b j z ) 2 − 2 a i z b j z = ( a i x ) 2 + ( a i y ) 2 + ( a i z ) 2 + ( b j x ) 2 + ( b j y ) 2 + ( b j z ) 2 − 2 a i x b j x − 2 a i y b j y − 2 a i z b j z \begin{aligned} (\red{a_i^x}-\blue{b_j^x})^2 + (\red{a_i^y}-\blue{b_j^y})^2 + (\red{a_i^z}-\blue{b_j^z})^2 &= (\red{a_i^x})^2+(\blue{b_j^x})^2 - 2\red{a_i^x}\blue{b_j^x} + (\red{a_i^y})^2+(\blue{b_j^y})^2 - 2\red{a_i^y}\blue{b_j^y} + (\red{a_i^z})^2+(\blue{b_j^z})^2 - 2\red{a_i^z}\blue{b_j^z} \\ &= (\red{a_i^x})^2+(\red{a_i^y})^2+(\red{a_i^z})^2+(\blue{b_j^x})^2+(\blue{b_j^y})^2+(\blue{b_j^z})^2 - 2\red{a_i^x}\blue{b_j^x} - 2\red{a_i^y}\blue{b_j^y} - 2\red{a_i^z}\blue{b_j^z} \end{aligned} (aix−bjx)2+(aiy−bjy)2+(aiz−bjz)2=(aix)2+(bjx)2−2aixbjx+(aiy)2+(bjy)2−2aiybjy+(aiz)2+(bjz)2−2aizbjz=(aix)2+(aiy)2+(aiz)2+(bjx)2+(bjy)2+(bjz)2−2aixbjx−2aiybjy−2aizbjz
为简化计算,我们假设 A A A 集合中有 m = 2 m=2 m=2 个点, B B B 集合中有 n = 3 n=3 n=3 个点。
A = [ a 1 x a 1 y a 1 z a 2 x a 2 y a 2 z ] 2 × 3 , B = [ b 1 x b 1 y b 1 z b 2 x b 2 y b 2 z b 3 x b 3 y b 3 z ] 3 × 3 A = \left[\begin{matrix} \red{a_1^x} & \red{a_1^y} & \red{a_1^z} \\ a_2^x & a_2^y & a_2^z \\ \end{matrix}\right]_{2 \times 3}, \quad B = \left[\begin{matrix} \blue{b_1^x} & \blue{b_1^y} & \blue{b_1^z} \\ b_2^x & b_2^y & b_2^z \\ b_3^x & b_3^y & b_3^z \\ \end{matrix}\right]_{3 \times 3} A=[a1xa2xa1ya2ya1za2z]2×3,B= b1xb2xb3xb1yb2yb3yb1zb2zb3z 3×3
计算过程如下:
- 先求出矩阵 A B T AB^\text{T} ABT 有:
A B T = [ a 1 x a 1 y a 1 z a 2 x a 2 y a 2 z ] [ b 1 x b 2 x b 3 z b 1 y b 2 y b 3 y b 1 z b 2 z b 3 z ] = [ a 1 x b 1 x + a 1 y b 1 y + a 1 z b 1 z a 1 x b 2 x + a 1 y b 2 y + a 1 z b 2 z a 1 x b 3 x + a 1 y b 3 y + a 1 z b 3 z a 2 x b 1 x + a 2 y b 1 y + a 2 z b 1 z a 2 x b 2 x + a 2 y b 2 y + a 2 z b 2 z a 2 x b 3 x + a 2 y b 3 y + a 2 z b 3 z ] \begin{aligned} AB^\text{T} &= \left[\begin{matrix} \red{a_1^x} & \red{a_1^y} & \red{a_1^z} \\ a_2^x & a_2^y & a_2^z \\ \end{matrix}\right] \left[\begin{matrix} \blue{b_1^x} & b_2^x & b_3^z \\ \blue{b_1^y} & b_2^y & b_3^y \\ \blue{b_1^z} & b_2^z & b_3^z \\ \end{matrix}\right] \\ &= \left[\begin{matrix} \red{a_1^x}\blue{b_1^x} + \red{a_1^y}\blue{b_1^y} + \red{a_1^z}\blue{b_1^z} && \red{a_1^x} b_2^x + \red{a_1^y} b_2^y + \red{a_1^z} b_2^z && \red{a_1^x} b_3^x + \red{a_1^y} b_3^y + \red{a_1^z} b_3^z \\ a_2^x \blue{b_1^x} + a_2^y \blue{b_1^y} + a_2^z \blue{b_1^z} && a_2^x b_2^x + a_2^y b_2^y + a_2^z b_2^z && a_2^x b_3^x + a_2^y b_3^y + a_2^z b_3^z \end{matrix}\right] \end{aligned} ABT=[a1xa2xa1ya2ya1za2z] b1xb1yb1zb2xb2yb2zb3zb3yb3z =[a1xb1x+a1yb1y+a1zb1za2xb1x+a2yb1y+a2zb1za1xb2x+a1yb2y+a1zb2za2xb2x+a2yb2y+a2zb2za1xb3x+a1yb3y+a1zb3za2xb3x+a2yb3y+a2zb3z]
- 然后分别对 A A A 和 B B B 求其中每个坐标点向量的模的平方,并扩展为 2 × 3 2 \times 3 2×3 矩阵。
- 对 A A A 中的 a 1 \red{a_1} a1 向量有 ∣ a 1 ∣ 2 = ( a 1 x ) 2 + ( a 1 y ) 2 + ( a 1 z ) 2 |\red{a_1}|^2 = (\red{a_1^x})^2 + (\red{a_1^y})^2 + (\red{a_1^z})^2 ∣a1∣2=(a1x)2+(a1y)2+(a1z)2
- 对 A A A 中的 a 2 a_2 a2 向量有 ∣ a 2 ∣ 2 = ( a 2 x ) 2 + ( a 2 y ) 2 + ( a 2 z ) 2 |a_2|^2 = (a_2^x)^2 + (a_2^y)^2 + (a_2^z)^2 ∣a2∣2=(a2x)2+(a2y)2+(a2z)2
- 对 B B B 中的 b 1 \blue{b_1} b1 向量有 ∣ b 1 ∣ 2 = ( b 1 x ) 2 + ( b 1 y ) 2 + ( b 1 z ) 2 |\blue{b_1}|^2 = (\blue{b_1^x})^2 + (\blue{b_1^y})^2 + (\blue{b_1^z})^2 ∣b1∣2=(b1x)2+(b1y)2+(b1z)2
- 对 B B B 中的 b 2 b_2 b2 向量有 ∣ b 2 ∣ 2 = ( b 2 x ) 2 + ( b 2 y ) 2 + ( b 2 z ) 2 |b_2|^2 = (b_2^x)^2 + (b_2^y)^2 + (b_2^z)^2 ∣b2∣2=(b2x)2+(b2y)2+(b2z)2
- 对 B B B 中的 b 3 b_3 b3 向量有 ∣ b 3 ∣ 2 = ( b 3 x ) 2 + ( b 3 y ) 2 + ( b 3 z ) 2 |b_3|^2 = (b_3^x)^2 + (b_3^y)^2 + (b_3^z)^2 ∣b3∣2=(b3x)2+(b3y)2+(b3z)2
A m s = [ ∣ a 1 ∣ 2 ∣ a 1 ∣ 2 ∣ a 1 ∣ 2 ∣ a 2 ∣ 2 ∣ a 2 ∣ 2 ∣ a 2 ∣ 2 ] A_{ms} = \left[\begin{matrix} |\red{a_1}|^2 && |\red{a_1}|^2 && |\red{a_1}|^2 \\ |a_2|^2 && |a_2|^2 && |a_2|^2 \\ \end{matrix}\right] Ams=[∣a1∣2∣a2∣2∣a1∣2∣a2∣2∣a1∣2∣a2∣2]
B m s = [ ∣ b 1 ∣ 2 ∣ b 2 ∣ 2 ∣ b 3 ∣ 2 ∣ b 1 ∣ 2 ∣ b 2 ∣ 2 ∣ b 3 ∣ 2 ] B_{ms} = \left[\begin{matrix} |\blue{b_1}|^2 && |b_2|^2 && |b_3|^2 \\ |\blue{b_1}|^2 && |b_2|^2 && |b_3|^2 \\ \end{matrix}\right] Bms=[∣b1∣2∣b1∣2∣b2∣2∣b2∣2∣b3∣2∣b3∣2]
- 计算矩阵 C = A m s + B m s − 2 A B T C = A_{ms} + B_{ms} - 2 A B^\text{T} C=Ams+Bms−2ABT
A m s + B m s − 2 A B T = [ ∣ a 1 ∣ 2 ∣ a 1 ∣ 2 ∣ a 1 ∣ 2 ∣ a 2 ∣ 2 ∣ a 2 ∣ 2 ∣ a 2 ∣ 2 ] + [ ∣ b 1 ∣ 2 ∣ b 2 ∣ 2 ∣ b 3 ∣ 2 ∣ b 1 ∣ 2 ∣ b 2 ∣ 2 ∣ b 3 ∣ 2 ] − 2 [ a 1 x b 1 x + a 1 y b 1 y + a 1 z b 1 z a 1 x b 2 x + a 1 y b 2 y + a 1 z b 2 z a 1 x b 3 x + a 1 y b 3 y + a 1 z b 3 z a 2 x b 1 x + a 2 y b 1 y + a 2 z b 1 z a 2 x b 2 x + a 2 y b 2 y + a 2 z b 2 z a 2 x b 3 x + a 2 y b 3 y + a 2 z b 3 z ] = [ ( a 1 x ) 2 + ( a 1 y ) 2 + ( a 1 z ) 2 ( a 1 x ) 2 + ( a 1 y ) 2 + ( a 1 z ) 2 ( a 1 x ) 2 + ( a 1 y ) 2 + ( a 1 z ) 2 ( a 2 x ) 2 + ( a 2 y ) 2 + ( a 2 z ) 2 ( a 2 x ) 2 + ( a 2 y ) 2 + ( a 2 z ) 2 ( a 2 x ) 2 + ( a 2 y ) 2 + ( a 2 z ) 2 ] + [ ( b 1 x ) 2 + ( b 1 y ) 2 + ( b 1 z ) 2 ( b 2 x ) 2 + ( b 2 y ) 2 + ( b 2 z ) 2 ( b 3 x ) 2 + ( b 3 y ) 2 + ( b 3 z ) 2 ( b 1 x ) 2 + ( b 1 y ) 2 + ( b 1 z ) 2 ( b 2 x ) 2 + ( b 2 y ) 2 + ( b 2 z ) 2 ( b 3 x ) 2 + ( b 3 y ) 2 + ( b 3 z ) 2 ] − 2 [ a 1 x b 1 x + a 1 y b 1 y + a 1 z b 1 z a 1 x b 2 x + a 1 y b 2 y + a 1 z b 2 z a 1 x b 3 x + a 1 y b 3 y + a 1 z b 3 z a 2 x b 1 x + a 2 y b 1 y + a 2 z b 1 z a 2 x b 2 x + a 2 y b 2 y + a 2 z b 2 z a 2 x b 3 x + a 2 y b 3 y + a 2 z b 3 z ] = [ ( a 1 x ) 2 + ( a 1 y ) 2 + ( a 1 z ) 2 + ( b 1 x ) 2 + ( b 1 y ) 2 + ( b 1 z ) 2 − 2 a 1 x b 1 x − 2 a 1 y b 1 y − 2 a 1 z b 1 z ⋯ ⋯ ( a 2 x ) 2 + ( a 2 y ) 2 + ( a 2 z ) 2 + ( b 1 x ) 2 + ( b 1 y ) 2 + ( b 1 z ) 2 − 2 a 2 x b 1 x − 2 a 2 y b 1 y − 2 a 2 z b 1 z ⋯ ⋯ ] \begin{aligned} A_{ms} + B_{ms} - 2 A B^\text{T} &= \left[\begin{matrix} |\red{a_1}|^2 && |\red{a_1}|^2 && |\red{a_1}|^2 \\ |a_2|^2 && |a_2|^2 && |a_2|^2 \\ \end{matrix}\right] + \left[\begin{matrix} |\blue{b_1}|^2 && |b_2|^2 && |b_3|^2 \\ |\blue{b_1}|^2 && |b_2|^2 && |b_3|^2 \\ \end{matrix}\right] \\ &- 2\left[\begin{matrix} \red{a_1^x}\blue{b_1^x} + \red{a_1^y}\blue{b_1^y} + \red{a_1^z}\blue{b_1^z} && \red{a_1^x} b_2^x + \red{a_1^y} b_2^y + \red{a_1^z} b_2^z && \red{a_1^x} b_3^x + \red{a_1^y} b_3^y + \red{a_1^z} b_3^z \\ a_2^x \blue{b_1^x} + a_2^y \blue{b_1^y} + a_2^z \blue{b_1^z} && a_2^x b_2^x + a_2^y b_2^y + a_2^z b_2^z && a_2^x b_3^x + a_2^y b_3^y + a_2^z b_3^z \end{matrix}\right] \\ \\ &= \left[\begin{matrix} (\red{a_1^x})^2 + (\red{a_1^y})^2 + (\red{a_1^z})^2 && (\red{a_1^x})^2 + (\red{a_1^y})^2 + (\red{a_1^z})^2 && (\red{a_1^x})^2 + (\red{a_1^y})^2 + (\red{a_1^z})^2 \\ (a_2^x)^2 + (a_2^y)^2 + (a_2^z)^2 && (a_2^x)^2 + (a_2^y)^2 + (a_2^z)^2 && (a_2^x)^2 + (a_2^y)^2 + (a_2^z)^2 \\ \end{matrix}\right] \\ &+ \left[\begin{matrix} (\blue{b_1^x})^2 + (\blue{b_1^y})^2 + (\blue{b_1^z})^2 && (b_2^x)^2 + (b_2^y)^2 + (b_2^z)^2 && (b_3^x)^2 + (b_3^y)^2 + (b_3^z)^2 \\ (\blue{b_1^x})^2 + (\blue{b_1^y})^2 + (\blue{b_1^z})^2 && (b_2^x)^2 + (b_2^y)^2 + (b_2^z)^2 && (b_3^x)^2 + (b_3^y)^2 + (b_3^z)^2 \\ \end{matrix}\right] \\ &- 2\left[\begin{matrix} \red{a_1^x}\blue{b_1^x} + \red{a_1^y}\blue{b_1^y} + \red{a_1^z}\blue{b_1^z} && \red{a_1^x} b_2^x + \red{a_1^y} b_2^y + \red{a_1^z} b_2^z && \red{a_1^x} b_3^x + \red{a_1^y} b_3^y + \red{a_1^z} b_3^z \\ a_2^x \blue{b_1^x} + a_2^y \blue{b_1^y} + a_2^z \blue{b_1^z} && a_2^x b_2^x + a_2^y b_2^y + a_2^z b_2^z && a_2^x b_3^x + a_2^y b_3^y + a_2^z b_3^z \end{matrix}\right] \\ \\ &= \left[\begin{matrix} (\red{a_1^x})^2 + (\red{a_1^y})^2 + (\red{a_1^z})^2 +(\blue{b_1^x})^2 + (\blue{b_1^y})^2 + (\blue{b_1^z})^2 -2\red{a_1^x}\blue{b_1^x} - 2\red{a_1^y}\blue{b_1^y} - 2\red{a_1^z}\blue{b_1^z} && \cdots && \cdots \\ (a_2^x)^2 + (a_2^y)^2 + (a_2^z)^2 + (\blue{b_1^x})^2 + (\blue{b_1^y})^2 + (\blue{b_1^z})^2 - 2a_2^x \blue{b_1^x} - 2a_2^y \blue{b_1^y} - 2a_2^z \blue{b_1^z} && \cdots && \cdots \\ \end{matrix}\right] \end{aligned} Ams+Bms−2ABT=[∣a1∣2∣a2∣2∣a1∣2∣a2∣2∣a1∣2∣a2∣2]+[∣b1∣2∣b1∣2∣b2∣2∣b2∣2∣b3∣2∣b3∣2]−2[a1xb1x+a1yb1y+a1zb1za2xb1x+a2yb1y+a2zb1za1xb2x+a1yb2y+a1zb2za2xb2x+a2yb2y+a2zb2za1xb3x+a1yb3y+a1zb3za2xb3x+a2yb3y+a2zb3z]=[(a1x)2+(a1y)2+(a1z)2(a2x)2+(a2y)2+(a2z)2(a1x)2+(a1y)2+(a1z)2(a2x)2+(a2y)2+(a2z)2(a1x)2+(a1y)2+(a1z)2(a2x)2+(a2y)2+(a2z)2]+[(b1x)2+(b1y)2+(b1z)2(b1x)2+(b1y)2+(b1z)2(b2x)2+(b2y)2+(b2z)2(b2x)2+(b2y)2+(b2z)2(b3x)2+(b3y)2+(b3z)2(b3x)2+(b3y)2+(b3z)2]−2[a1xb1x+a1yb1y+a1zb1za2xb1x+a2yb1y+a2zb1za1xb2x+a1yb2y+a1zb2za2xb2x+a2yb2y+a2zb2za1xb3x+a1yb3y+a1zb3za2xb3x+a2yb3y+a2zb3z]=[(a1x)2+(a1y)2+(a1z)2+(b1x)2+(b1y)2+(b1z)2−2a1xb1x−2a1yb1y−2a1zb1z(a2x)2+(a2y)2+(a2z)2+(b1x)2+(b1y)2+(b1z)2−2a2xb1x−2a2yb1y−2a2zb1z⋯⋯⋯⋯]文章来源:https://www.toymoban.com/news/detail-516312.html
- 将得到的矩阵 C C C 对每个开平方,就得到了 A , B A, B A,B 向量集两两间的欧式距离了。同时, D D D 中的元素 d i j d_{ij} dij 表示 A A A 集合中 a i a_i ai 点到 B B B 集合中点 b j b_j bj 的距离。
D = C ( 这个公式写法并不严谨,但表述意思 ) D = \sqrt{C} \quad(这个公式写法并不严谨,但表述意思) D=C(这个公式写法并不严谨,但表述意思)文章来源地址https://www.toymoban.com/news/detail-516312.html
A = [1 2 3
4 5 6];
B = [1 2 3
4 5 6
7 8 9];
% 1. A * B'
% 2. repmat(sum(A.^2,2), 1,size(B,1))
% 2. repmat(sum(B.^2,2)',size(A,1),1)
% 3.
C = repmat(sum(A.^2,2), 1,size(B,1))...
+ repmat(sum(B.^2,2)',size(A,1),1)...
- 2*A*B';
% 4.
D = sqrt(C);
>> D
D =
0 5.1962 10.3923
5.1962 0 5.1962
Ref
- 求矩阵中向量两两间的欧氏距离(python实现)
- 差平方 - WikiPedia
到了这里,关于【数理知识】求两个三维空间点的坐标矩阵之间,任意两两点之间的空间距离,matlab 实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!