首页 > 解决方案 > 计算 voronoi 的元胞数组

问题描述

我正在将 Matlab 代码转换为 python。我在 Matlab 中遇到了一个函数“voronoin”,它提供顶点和单元数组作为输出。从我的程序中,我只得到顶点。我从程序中获得的单元格数组与我在 Matlab 中获得的单元格数组不匹配。

import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi



def voronoi_volumes(points):
    v = Voronoi(points)
    print(v.vertices)
    for i, reg_num in enumerate(v.point_region):
        print(reg_num)
        indices = v.regions[reg_num]
        print(indices)


points = np.array([[ 0.4074,  0.4567],
                   [ 0.4529,  0.3162],
                   [ 0.0635,  0.0488],
                   [ 5, -45],
                   [ 5,  55],
                   [-45,  5],
                   [ 55,  5]])
k = voronoi_volumes(points)

在 Matlab 中,我的输出是:

[9,5,3,4,7]
[9,7,6,8]
[7,4,2,6]
[8,1,2,6]
[5,1,3]
[4,2,1,3]
[9,5,1,8]

在 python 中,我得到的输出是:

[7, 3, 1, 2, 5]
[7, 5, 4, 6]
[5, 2, 0, 4]
[6, -1, 0, 4]
[-1, 1, 3]
[-1, 0, 2, 1]
[7, 3, -1, 6]

标签: pythonmatlab

解决方案


我在 python 中得到的答案是正确的。Matlab 和 python 有不同的索引。当我尝试从索引访问顶点时,我从两者得到相同的答案。


推荐阅读