Python -Matrix -Diagonale von Inf ohne FILL_DIAGONALPython

Python-Programme
Anonymous
 Python -Matrix -Diagonale von Inf ohne FILL_DIAGONAL

Post by Anonymous »

Ich muss die diagonalen Elemente einer Matrix auf Inf.

Code: Select all

np.fill_diagonal(my_matrix, float('inf')
< /code>

FILL_DIAGONAL < /code> modifiziert die Eingangsmatrix, anstatt eine neue Matrix mit dem diagonalen gefüllten gefüllt zurückzugeben.
Dies funktioniert für mich nicht. Ich brauche die Diagonalen, die gefüllt sind, ohne die ursprüngliche Matrix zu modifizieren. Allerdings mag ich diese Lösung nicht wirklich, da ich meine ursprüngliche Matrix oft aktualisieren werde und daher jedes Mal, wenn ich die Diagonale benötige, um inf zu sein. So etwas wie: < /p>

new_matrix = np.fill_diagonal(original_matrix, float('inf')
< /code>

 Warum ich Folgendes brauche: < /strong> < /p>

Meine Matrix ist eine Entfernungsmatrix zwischen den Punkten und [url=viewtopic.php?t=14917]ich möchte[/url] bei jedem Schritt die beiden engsten Punkte berechnen. Natürlich beträgt die Diagonale dieser Matrix 0 (da der Abstand von einem Punkt zu sich selbst 0 beträgt). Meine Lösung, um sicherzustellen, dass ich nicht den gleichen Punkt nehme, besteht darin, die Diagonalen auf Inf zu setzen. /> 
 Füllen Sie Diagonale mit INF < /li>
 Finden Sie die 2 engsten Punkte < /li>
 Füllen Sie Diagonale mit 0 < /li>
  ZUSCHABEN SIE DEN EIGENSCHALTEN DIAGONALEN IN DIE ANDERETE INDEREN AND DIE REST INDEREN.# fill diagonal with Inf to avoid taking the diagonals
np.fill_diagonal(data, float('inf'))
# find the minimum distance
idx = np.argmin(data)
# fill the diagonals back to 0
np.fill_diagonal(data, 0.0)
# get the coordinates of the minimum distance
row, col =  np.unravel_index(idx,data.shape)
# compute the new node as the average distance between the two points
new_node = np.mean((data[:,row],data[:,col]),0)
# replace the first node (row) with the new node
data[:,row] = new_node
data[row,:] = new_node.T
# delete the second node (col) from the matrix
data = np.delete(data, col, 0)  # delete row
data = np.delete(data, col, 1)  # delete column
< /code> < /li>
< /ul>

Wie auch immer die Idee, Diagonale auf INF einzustellen, und dann würde ich es vorziehen, nur eine Funktion an Argmax zu übergeben.idx = np.argmin(return_filled_diagonals(data, float('Inf'))
# here I can operate with data as usual since it has not been modified.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post