矩阵向量中两两间欧式距离计算[通俗易懂]
生活随笔
收集整理的這篇文章主要介紹了
矩阵向量中两两间欧式距离计算[通俗易懂]
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目標(biāo):希望通過的矩陣運(yùn)算就能得出矩陣向量中兩兩之間的歐式距離
歐氏距離公式:
-
一般而言,我們常見的歐式距離計算公式如下:
- a,b 對應(yīng)的是兩組不同的向量
- d i s t ( a , b ) = ( a 1 ? b 1 ) 2 + ( a 2 ? b 2 ) 2 + ? ? ? ( a n ? b n ) 2 dist(a,b)=\sqrt{(a_1-b_1)^{2}+(a_2-b_2)^{2}+···(a_n-b_n)^{2}} dist(a,b)=(a1??b1?)2+(a2??b2?)2+???(an??bn?)2
?
-
事實(shí)上對于上面的公式如果我們通過向量的角度來考慮,就會變成是下列形式:
- a,b 對應(yīng)的是兩組不同的向量
- d i s t ( a , b ) = d o t ( a , a ) ? 2 ? d o t ( a , b ) + d o t ( b , b ) dist(a,b) = \sqrt{dot(a,a)-2*dot(a,b)+dot(b,b)} dist(a,b)=dot(a,a)?2?dot(a,b)+dot(b,b)
?
假設(shè)有下列矩陣 A A A:
-
A = [ a 1 a 2 a 3 b 1 b 2 b 3 ] {A}= \left[{\begin{array}{}{a{_1}}&{a{_2}}&{a{_3}}\\{b{_1}}&{b{_2}}&{b{_3}}\end{array}}\right] A=[a1?b1??a2?b2??a3?b3??]
-
為了湊出上面的公式:
-
先計算出 d o t ( A , A ) dot(A,A) dot(A,A) -> A A T {AA^T} AAT,
- A  ̄ = [ a 1 a 2 a 3 b 1 b 2 b 3 ] [ a 1 b 1 a 2 b 2 a 3 b 3 ] = [ ( a 1 ) 2 + ( a 1 ) 2 + ( a 1 ) 2 ( a 1 ) ( b 1 ) + ( a 2 ) ( b 2 ) + ( a 3 ) ( b 3 ) ( a 1 ) ( b 1 ) + ( a 2 ) ( b 2 ) + ( a 3 ) ( b 3 ) ( b 1 ) 2 + ( b 2 ) 2 + ( b 3 ) 2 ] \overline{A} = \left[{\begin{array}{}{a{_1}}&{a{_2}}&{a{_3}}\\{b{_1}}&{b{_2}}&{b{_3}}\end{array}}\right] \left[{\begin{array}{}{a{_1}}&{b{_1}} \\ {a{_2}}&{b{_2}} \\ {a{_3}}&{b{_3}} \end{array}}\right] = \left[{\begin{array}{}{(a{_1})^2+(a{_1})^2+(a{_1})^2}&{(a{_1})(b{_1})+(a{_2})(b{_2})+(a{_3})(b{_3})} \\{(a{_1})(b{_1})+(a{_2})(b{_2})+(a{_3})(b{_3})} & {(b{_1})^2+(b{_2})^2+(b{_3})^2} \end{array}}\right] A=[a1?b1??a2?b2??a3?b3??]???a1?a2?a3??b1?b2?b3?????=[(a1?)2+(a1?)2+(a1?)2(a1?)(b1?)+(a2?)(b2?)+(a3?)(b3?)?(a1?)(b1?)+(a2?)(b2?)+(a3?)(b3?)(b1?)2+(b2?)2+(b3?)2?]
-
取 A  ̄ \overline{A} A對角線:
- A  ̄ . d i a g ( ) \overline{A}.diag() A.diag() = [ ( a 1 ) 2 + ( a 2 ) 2 + ( a 3 ) 2 ( b 1 ) 2 + ( b 2 ) 2 + ( b 3 ) 2 ] \left[{\begin{array}{}{(a{_1})^2+(a{_2})^2+(a{_3})^2} & {(b{_1})^2+(b{_2})^2+(b{_3})^2}\end{array}}\right] [(a1?)2+(a2?)2+(a3?)2?(b1?)2+(b2?)2+(b3?)2?]
-
對對角線矩陣進(jìn)行一些變換
- A  ̄ 1 \overline{A}{_1} A1? = [ ( a 1 ) 2 + ( a 2 ) 2 + ( a 3 ) 2 ( b 1 ) 2 + ( b 2 ) 2 + ( b 3 ) 2 ] T \left[{\begin{array}{}{(a{_1})^2+(a{_2})^2+(a{_3})^2} & {(b{_1})^2+(b{_2})^2+(b{_3})^2}\end{array}}\right]^T [(a1?)2+(a2?)2+(a3?)2?(b1?)2+(b2?)2+(b3?)2?]T [ 1 1 ] \left[{\begin{array}{}1 & 1\end{array}}\right] [1?1?]
- = [ ( a 1 ) 2 + ( a 2 ) 2 + ( a 3 ) 2 ( a 1 ) 2 + ( a 2 ) 2 + ( a 3 ) 2 ( b 1 ) 2 + ( b 2 ) 2 + ( b 3 ) 2 ( b 1 ) 2 + ( b 2 ) 2 + ( b 3 ) 2 ] \left[{\begin{array}{}{(a{_1})^2+(a{_2})^2+(a{_3})^2} & {(a{_1})^2+(a{_2})^2+(a{_3})^2} \\ {(b{_1})^2+(b{_2})^2+(b{_3})^2} & {(b{_1})^2+(b{_2})^2+(b{_3})^2} \end{array}}\right] [(a1?)2+(a2?)2+(a3?)2(b1?)2+(b2?)2+(b3?)2?(a1?)2+(a2?)2+(a3?)2(b1?)2+(b2?)2+(b3?)2?]
- A  ̄ 2 \overline{A}{_2} A2? = [ 1 1 ] T \left[{\begin{array}{}1 & 1\end{array}}\right]^T [1?1?]T [ ( a 1 ) 2 + ( a 2 ) 2 + ( a 3 ) 2 ( b 1 ) 2 + ( b 2 ) 2 + ( b 3 ) 2 ] \left[{\begin{array}{}{(a{_1})^2+(a{_2})^2+(a{_3})^2} & {(b{_1})^2+(b{_2})^2+(b{_3})^2}\end{array}}\right] [(a1?)2+(a2?)2+(a3?)2?(b1?)2+(b2?)2+(b3?)2?]
- = [ ( a 1 ) 2 + ( a 2 ) 2 + ( a 3 ) 2 ( b 1 ) 2 + ( b 2 ) 2 + ( b 3 ) 2 ( a 1 ) 2 + ( a 2 ) 2 + ( a 3 ) 2 ( b 1 ) 2 + ( b 2 ) 2 + ( b 3 ) 2 ] \left[{\begin{array}{}{(a{_1})^2+(a{_2})^2+(a{_3})^2} & {(b{_1})^2+(b{_2})^2+(b{_3})^2} \\ {(a{_1})^2+(a{_2})^2+(a{_3})^2} & {(b{_1})^2+(b{_2})^2+(b{_3})^2} \end{array}}\right] [(a1?)2+(a2?)2+(a3?)2(a1?)2+(a2?)2+(a3?)2?(b1?)2+(b2?)2+(b3?)2(b1?)2+(b2?)2+(b3?)2?]
-
經(jīng)過了上面的處理,我們就可以得出上述的公式了
- d i s t ( A ) = A  ̄ 1 + A  ̄ 2 ? 2 A  ̄ dist(A) = {\overline{A}{_1}} + {\overline{A}{_2}} – {\overline{2A}} dist(A)=A1?+A2??2A
-
總結(jié)
以上是生活随笔為你收集整理的矩阵向量中两两间欧式距离计算[通俗易懂]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Betty's Sales team B
- 下一篇: S905系列的uboot分析