numpy.meshgrid
仿照Matlab的meshgrid
命令。它是用來vectorise兩個變量的函數,這樣你可以寫
x = numpy.array([1, 2, 3])
y = numpy.array([10, 20, 30])
XX, YY = numpy.meshgrid(x, y)
ZZ = XX + YY
ZZ => array([[11, 12, 13],
[21, 22, 23],
[31, 32, 33]])
所以ZZ
包含裝進功能的x
和y
所有組合。當你想到它時,meshgrid
對於numpy數組在播放時有點多餘。這意味着你可以做
XX, YY = numpy.atleast_2d(x, y)
YY = YY.T # transpose to allow broadcasting
ZZ = XX + YY
並得到相同的結果。
mgrid
和ogrid
是其使用索引符號,這樣就可以創建直接在前面的例子XX
和YY
,而無需使用類似linspace
輔助類。產生輸出的順序是相反的。
YY, XX = numpy.mgrid[10:40:10, 1:4]
ZZ = XX + YY # These are equivalent to the output of meshgrid
YY, XX = numpy.ogrid[10:40:10, 1:4]
ZZ = XX + YY # These are equivalent to the atleast_2d example
我不熟悉scitools的東西,但似乎ndgrid
相當於meshgrid
,而BoxGrid
實際上是全班同學來幫助這種產生。
這不是一個有建設性的問題嗎? – aquirdturtle
@aquirdturtle。我想知道這一點。對問題的回覆數量很好地表明瞭它的實用性。這些文檔並不盡如人意。 –