如果你有scipy,你对大多数matlab核心函数都有精确的等价:
from numpy import random, nanmax, argmax, unravel_index
from scipy.spatial.distance import pdist, squareform
A = random.randint(-5,5, (500,3))
D = pdist(A)
D = squareform(D);
N, [I_row, I_col] = nanmax(D), unravel_index( argmax(D), D.shape )
您也可以使用itertools在纯python中获取它:
from itertools import combinations
from random import randint
A = [[randint(-5,5) for coord in range(3)] for point in range(500)]
def square_distance(x,y): return sum([(xi-yi)**2 for xi, yi in zip(x,y)])
max_square_distance = 0
for pair in combinations(A,2):
if square_distance(*pair) > max_square_distance:
max_square_distance = square_distance(*pair)
max_pair = pair