

D1=(INDEX($A$1:$A$6,COLUMN()-3,)-$A1)^2+(INDEX($B$1:$B$6,COLUMN()-3,)-$B1)^2
資料量大時,可考慮VBA
Sub distancematrix()
Dim Fn As Object
Set Fn = Application.WorksheetFunction
Range("A1:B6").Select
nrow = Selection.Rows.Count
ncol = Selection.Columns.Count
For i = 1 To nrow
xtemp1 = Fn.Index(Selection.Value, i)
For j = 1 To nrow
xtemp2 = Fn.Index(Selection.Value, j)
distance = (Fn.Index(Fn.MMult(xtemp1, Fn.Transpose(xtemp1)), 1) - Fn.Index(Fn.MMult(xtemp1, Fn.Transpose(xtemp2)), 1) - Fn.Index(Fn.MMult(xtemp2, Fn.Transpose(xtemp1)), 1) + Fn.Index(Fn.MMult(xtemp2, Fn.Transpose(xtemp2)), 1)) ^ 0.5
Sheets("sheet2").Cells(i, j) = distance
Next
Next
End Sub
讀者回應 ( 0 意見 )
訂閱發佈留言 (Atom)
發佈留言
Please leave your name and tell me what you thought about this site. Comments, suggestions and views are welcomed.
如果這篇文章對你有幫助,那請留個訊息給我~