如何通过Python的scipy.spatial.Voronoi获得与MATLAB相同的voronoin输出
我使用MATLAB的如何通过Python的scipy.spatial.Voronoi获得与MATLAB相同的voronoin输出,python,matlab,scipy,voronoi,Python,Matlab,Scipy,Voronoi,我使用MATLAB的voronoin来判断单元之间的连接,我想把这个函数转换成Python 当我使用Python的scipy.spatial.Voronoi时,输出略有不同。 例如,我对MATLAB和Python使用了与您在下一段代码中看到的相同的输入 MATLAB: seed = [ 17.746 -0.37283 -0.75523; 6.1704 1.3404 7.0341; -7.7211 5.4282 4.501
voronoin
来判断单元之间的连接,我想把这个函数转换成Python
当我使用Python的scipy.spatial.Voronoi
时,输出略有不同。
例如,我对MATLAB和Python使用了与您在下一段代码中看到的相同的输入
MATLAB:
seed = [ 17.746 -0.37283 -0.75523;
6.1704 1.3404 7.0341;
-7.7211 5.4282 4.5016;
5.8014 2.1252 -6.2491;
-16.047 -2.8472 -0.024795;
-2.2967 -6.7334 0.60707]
[vvern_mat, vceln_mat] = voronoin(seed);
vvern_mat =
Inf Inf Inf
-6.9386 1.7980 -7.7861
-15.9902 -20.8031 50.1840
29.5016 106.3690 5.9214
8.6816 -6.5899 -0.1741
-0.2027 2.1210 0.5874
vceln_mat =
1 4 5
1 3 4 5 6
1 2 3 4 6
1 2 4 5 6
1 2 3
1 2 3 5 6
Python:
import numpy as np
from scipy.spatial import Voronoi
seed = np.array([[ 17.746 , -0.37283 , -0.75523 ],
[ 6.1704 , 1.3404 , 7.0341 ],
[ -7.7211 , 5.4282 , 4.5016 ],
[ 5.8014 , 2.1252 , -6.2491 ],
[-16.047 , -2.8472 , -0.024795],
[ -2.2967 , -6.7334 , 0.60707 ]])
vor = Voronoi(seed)
vvern_py = vor.vertices
vceln_py = vor.regions
vvern_py = array([[ -6.93864391, 1.79801934, -7.78610533],
[-15.9902125 , -20.80310202, 50.1840397 ],
[ 29.501584 , 106.36899584, 5.92137852],
[ 8.68156407, -6.58985621, -0.17410448],
[ -0.20266123, 2.12100225, 0.58735065]])
vceln_py = [[],
[-1, 0, 2, 3, 4],
[-1, 2, 3],
[-1, 0, 1],
[-1, 0, 1, 2, 4],
[-1, 1, 2, 3, 4],
[-1, 0, 1, 3, 4]]
结果如下:
MATLAB:
seed = [ 17.746 -0.37283 -0.75523;
6.1704 1.3404 7.0341;
-7.7211 5.4282 4.5016;
5.8014 2.1252 -6.2491;
-16.047 -2.8472 -0.024795;
-2.2967 -6.7334 0.60707]
[vvern_mat, vceln_mat] = voronoin(seed);
vvern_mat =
Inf Inf Inf
-6.9386 1.7980 -7.7861
-15.9902 -20.8031 50.1840
29.5016 106.3690 5.9214
8.6816 -6.5899 -0.1741
-0.2027 2.1210 0.5874
vceln_mat =
1 4 5
1 3 4 5 6
1 2 3 4 6
1 2 4 5 6
1 2 3
1 2 3 5 6
Python:
import numpy as np
from scipy.spatial import Voronoi
seed = np.array([[ 17.746 , -0.37283 , -0.75523 ],
[ 6.1704 , 1.3404 , 7.0341 ],
[ -7.7211 , 5.4282 , 4.5016 ],
[ 5.8014 , 2.1252 , -6.2491 ],
[-16.047 , -2.8472 , -0.024795],
[ -2.2967 , -6.7334 , 0.60707 ]])
vor = Voronoi(seed)
vvern_py = vor.vertices
vceln_py = vor.regions
vvern_py = array([[ -6.93864391, 1.79801934, -7.78610533],
[-15.9902125 , -20.80310202, 50.1840397 ],
[ 29.501584 , 106.36899584, 5.92137852],
[ 8.68156407, -6.58985621, -0.17410448],
[ -0.20266123, 2.12100225, 0.58735065]])
vceln_py = [[],
[-1, 0, 2, 3, 4],
[-1, 2, 3],
[-1, 0, 1],
[-1, 0, 1, 2, 4],
[-1, 1, 2, 3, 4],
[-1, 0, 1, 3, 4]]
当您关注vceln
时,您会注意到MATLAB和Python之间的值是相同的,因为您可以通过在vceln\u py
中添加两个来获得vceln\u mat
。
但是,行顺序不同,我很难将vceln_py
转换为vceln_mat
我认为我可以通过将MATLAB的Qhull
选项应用到Python中来解决这个问题,但是我无法获得相同的输出。(关于沃罗宁的选项:)
如果有人能解决这个问题,我将不胜感激。在
vor.regions
中列表的顺序可以是任意的。但是,您可以通过vor.point\u region
属性获取哪个区域与哪个入口点关联的信息。警察说
因此,您必须根据vor.point\u region
# Find point coordinate for each region
sorting = [np.where(vor.point_region==x)[0][0] for x in range(1, len(vor.regions))]
# sort regions along coordinate list `sorting` (exclude first, empty list [])
sorted_regions = [x for _, x in sorted(zip(sorting, vor.regions[1:]))]
sorted_regions = [[-1, 2, 3],
[-1, 1, 2, 3, 4],
[-1, 0, 1, 2, 4],
[-1, 0, 2, 3, 4],
[-1, 0, 1],
[-1, 0, 1, 3, 4]]
这样,您就得到了MATLABvoronoin
函数的排序,这显然已经本质上完成了排序
为了得到同样的数值,您可以计算(正如您已经提到的那样)
然而,其原因似乎既没有记录在文档中,也没有记录在文档中。结果中的行顺序应该无关紧要,因为它取决于算法在引擎盖下执行的操作。我建议您以某种方式对vceln_*的输出进行排序,以便获得相同的结果。因为每一行都与特定的输入点相关联,所以排序很重要。请参阅我的答案以了解有关此的详细信息谢谢@gehbiszumeis。您已经能够应用我刚才提到的“以某种方式对输出进行排序”。)