python - 计算 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]
解决方案
我在 python 中得到的答案是正确的。Matlab 和 python 有不同的索引。当我尝试从索引访问顶点时,我从两者得到相同的答案。
推荐阅读
- haskell - 如何显示两个输入是否相同
- python - 在类构造函数中定义逻辑以根据python中的其他构造函数参数修改实例变量
- json - Scala 映射选项
- rxdatasources - 如果 UITableview 中有大量数据,则 BehaviorRelay/Variable 缓慢的 RxDataSources
- css - 使用 sass 自动增加类特异性
- html - 如何在 CSS 网格中偏移 div
- servlets - 在 felix jetty 中部署包时,Servlet 类不是 javax.servlet.Servlet
- apache-nifi - 一起处理传入队列中存在的 nifi 流文件
- protocol-buffers - 由于默认值,Protobuf 忽略 bool 和 ints 值
- javascript - angularjs中我的jsp中其他控制器的可访问性