我有一個列表,我用它來存儲對象之間的距離。確定一個NxN數組的行和列的最低值
表看起來是這樣的:
+----------+----------+----------+----------+----------+
| | Object_A | Object_B | Object_C | Object_D |
+----------+----------+----------+----------+----------+
| Entity_E | 2 | 3 | 6 | 1 |
+----------+----------+----------+----------+----------+
| Entity_F | 3 | 4 | 7 | 2 |
+----------+----------+----------+----------+----------+
| Entity_G | 9 | 1 | 2 | 3 |
+----------+----------+----------+----------+----------+
的數字表示該行&列標題之間的距離。
這是粗略計算如下:
entites = [Entity_E, Entity_F, Entity_G]
objects = [Object_A, Object_B, Object_C, Obhect_D]
distances = []
for object in objects:
distanceEntry = []
for entity in entities:
distance = getDistance(entity, object)
distanceEntry.append(distance)
distances.append(distanceEntry)
這給了我大約在上表中的信息。
我在尋找的是基本上找到每個實體的最接近的對象(反之亦然)。每個對象或實體只能與另一個對象匹配,並且應該基於鄰近性。
他們這樣做的方式現在是通過按距離大小排序嵌套列表(在完整的代碼中,我有一種確定每個距離與哪個對象關聯的方式)。
所以,在這樣做,我將創建下列協會:
+----------+----------+----------+
| Entity | Object | Distance |
+----------+----------+----------+
| Entity_E | Object_D | 1 |
+----------+----------+----------+
| Entity_F | Object_D | 2 |
+----------+----------+----------+
| Entity_G | Object_B | 1 |
+----------+----------+----------+
這是不正確,因爲這兩次關聯Object_D。
該協會應該是:
+----------+----------+----------+
| Entity | Object | Distance |
+----------+----------+----------+
| Entity_E | Object_D | 1 |
+----------+----------+----------+
| Entity_F | Object_A | 3 |
+----------+----------+----------+
| Entity_G | Object_B | 1 |
+----------+----------+----------+
這是我掙扎 - 我不是最好的方法找出該代碼邏輯這一點。
由於Entity_E更接近Object_D,它應該獲得關聯。所以,對於Entity_F,我需要排在第二位。
我正在考慮做一些事情,我會記錄哪些對象已被分配,或者嘗試做一些事情,首先匹配每列中的最小值,但它們似乎都會遇到問題。
是否有矩陣運算或某種矩陣運算可用於計算?
任何意見將不勝感激!
編輯 - 添加全碼:
# Create an array that stores the distances between each label and symbol. Only calculate the distance for label that
# are "in front" of the symbol.
# Example Table:
# +---------------+--------------+--------------+--------------+--------------+
# | | Label_1 | Label_2 | Label_3 | Label_4 |
# +---------------+--------------+--------------+--------------+--------------+
# | Measurement_1 | 2 | 3 | Not_in_front | 1 |
# +---------------+--------------+--------------+--------------+--------------+
# | Measurement_2 | 3 | 4 | 1 | Not_in_front |
# +---------------+--------------+--------------+--------------+--------------+
# | Measurement_3 | Not_in_front | Not_in_front | 2 | 1 |
# +---------------+--------------+--------------+--------------+--------------+
# Data Structures:
# measurementsDictionary = {['Type', 'Handle', 'X-Coord', 'Y-Coord', 'Z-Coord', 'Rotation', 'True Strike']}
# dipsDictionary = {['Handle', 'Text', 'Unit', 'X-Coord', 'Y-Coord', 'Z-Coord']}
#The two functions below grab the information from a csv-like file.
measurementsDictionary = getMeasurementsInformationFromFile()
dipsDictionary = getDipsInformationFromFile()
exportHeaders = [" "]
exportArray = []
for measurementSymbol in measurementsDictionary:
measurementEntry = measurementsDictionary[measurementSymbol]
measurementCoord = [measurementEntry[2], measurementEntry[3]]
measurementDistance = []
measurementDistance.append(measurementEntry[1])
measurementCartesianAngle = getCartesianAngle(measurementEntry[6])
measurementLineEquation = generateLineEquation(measurementCoord,measurementCartesianAngle)
for dip in dipsDictionary:
dipEntry = dipsDictionary[dip]
dipCoord = [dipEntry[3],dipEntry[4]]
isPointInFrontOfLineTest = isPointInFrontOfLine(measurementCartesianAngle, measurementLineEquation, dipCoord)
if isPointInFrontOfLineTest == 1:
measurementSymbolDistance = calculateDistance(measurementCoord, dipCoord)
# string = dipEntry[0] +":" + str(measurementSymbolDistance)
# measurementDistance.append(string)
measurementDistance.append(measurementSymbolDistance)
elif isPointInFrontOfLineTest == 0:
string = ""
measurementDistance.append(string)
exportArray.append(measurementDistance)
for dip in dipsDictionary:
dipEntry = dipsDictionary[dip]
exportHeaders.append(dipEntry[0])
exportedArray = [exportHeaders] + exportArray
export = np.array(exportedArray)
np.savetxt("exportArray2.csv", export, fmt='%5s', delimiter=",")
print(exportHeaders)
你能添加完整的代碼嗎? – sera
盡我所能編輯和添加儘可能多的代碼! – labourday