Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么meshgrid对输出维度的排序与对输入的排序不同?_Python_Numpy - Fatal编程技术网

Python 为什么meshgrid对输出维度的排序与对输入的排序不同?

Python 为什么meshgrid对输出维度的排序与对输入的排序不同?,python,numpy,Python,Numpy,我试图理解meshgrid是如何根据输入对输出的维度进行索引的。我注意到它总是将前两个维度转换。例如,当提供长度为7的x和长度为6的y时,它将以6x7数组而不是7x6数组的形式返回输出数组 这显然是设计的,所以我试图理解为什么/逻辑,以便我可以按预期使用它。有人能照一下这个吗?谢谢 import numpy as np #specify input dimensions of different lengths to aid in identifying which output index

我试图理解meshgrid是如何根据输入对输出的维度进行索引的。我注意到它总是将前两个维度转换。例如,当提供长度为7的x和长度为6的y时,它将以6x7数组而不是7x6数组的形式返回输出数组

这显然是设计的,所以我试图理解为什么/逻辑,以便我可以按预期使用它。有人能照一下这个吗?谢谢

import numpy as np

#specify input dimensions of different lengths to aid in identifying which output index a dimension belongs to
x = np.linspace(0,60,7)
y = np.linspace(0,50,6)
z = np.linspace(0,40,5)
i = np.linspace(0,30,4)
j = np.linspace(0,20,3)

#2D mesh grid, output dimensions transposed from input
xx, yy = np.meshgrid(x,y)
print(xx.shape)
print(yy.shape)

#3D mesh grid, first two output dimensions transposed from input
xx, yy, zz = np.meshgrid(x,y,z)
print(xx.shape)
print(yy.shape)
print(zz.shape)

#4D meshgrid, first two output dimensions transposed from input, rest are in input order
xx, yy, zz, ii = np.meshgrid(x,y,z,i)
print(xx.shape)
print(yy.shape)
print(zz.shape)
print(ii.shape)

#5D meshgrid, first two output dimensions transposed from input, rest are in input order
xx, yy, zz, ii, jj = np.meshgrid(x,y,z,i,j)
print(xx.shape)
print(yy.shape)
print(zz.shape)
print(ii.shape)
print(jj.shape)

如果将“索引”选项设置为“ij”,则可以执行此操作。此功能是在numpy版本1.7.0中添加的。


如果将“索引”选项设置为“ij”,则可以执行此操作。此功能是在numpy版本1.7.0中添加的。


我认为这是轴坐标和矩阵坐标的区别。当你想到参考坐标系中的一组轴时,首先提到x,然后才提到y是有意义的。例如,我们都学过y=f(x)。然而,对于矩阵,我们知道我们需要首先按行索引:(0,1)是第二列的第一个元素。现在,如果您尝试在图形上表示矩阵,则会出现问题,因为x轴将对应于列,y轴对应于行:存在不匹配。因此,如果要获得一个(7,6)数组并简单地将其粘贴到图形上,那么坐标将不匹配,因为列值将是y坐标。我认为,这就是为什么需要进行转置操作,以便在矩阵表示和轴坐标之间保持一致。

我认为这就是轴坐标和矩阵坐标之间的差异。当你想到参考坐标系中的一组轴时,首先提到x,然后才提到y是有意义的。例如,我们都学过y=f(x)。然而,对于矩阵,我们知道我们需要首先按行索引:(0,1)是第二列的第一个元素。现在,如果您尝试在图形上表示矩阵,则会出现问题,因为x轴将对应于列,y轴对应于行:存在不匹配。因此,如果要获得一个(7,6)数组并简单地将其粘贴到图形上,那么坐标将不匹配,因为列值将是y坐标。我相信,这就是为什么需要进行转置操作,以使矩阵表示和轴坐标保持一致。

您可以控制顺序-阅读文档您可以控制顺序-阅读文档,它解释了我所寻找的洞察力和“原因”。谢谢当超越两个维度时,我想从轴的角度思考不再有效,因为我们无法将其可视化,这就是为什么额外维度(第三、第四等)恢复为根据矩阵索引排序的原因?我只想说,添加更多维度不是问题,因为它们不会产生任何不匹配。但事实上,当你引入第四维度时,这个可视化的例子就不起作用了。这就解释了我想要的洞察力和“为什么”。谢谢当超越两个维度时,我想从轴的角度思考不再有效,因为我们无法将其可视化,这就是为什么额外维度(第三、第四等)恢复为根据矩阵索引排序的原因?我只想说,添加更多维度不是问题,因为它们不会产生任何不匹配。但实际上,当你引入第四维度时,这个可视化的例子就停止工作了。
import numpy as np

#specify input dimensions of different lengths to aid in identifying which index dimension belongs to in output
x = np.linspace(0,60,7)
y = np.linspace(0,50,6)
z = np.linspace(0,40,5)
i = np.linspace(0,30,4)
j = np.linspace(0,20,3)

#2D mesh grid, output dimensions transposed from input
xx, yy = np.meshgrid(x,y,indexing='ij')
print(xx.shape)
print(yy.shape)

#3D mesh grid, first two output dimensions transposed from input
xx, yy, zz = np.meshgrid(x,y,z,indexing='ij')
print(xx.shape)
print(yy.shape)
print(zz.shape)

#4D meshgrid, first two output dimensions transposed from input, rest are in input order
xx, yy, zz, ii = np.meshgrid(x,y,z,i,indexing='ij')
print(xx.shape)
print(yy.shape)
print(zz.shape)
print(ii.shape)

#5D meshgrid, first two output dimensions transposed from input, rest are in input order
xx, yy, zz, ii, jj = np.meshgrid(x,y,z,i,j,indexing='ij')
print(xx.shape)
print(yy.shape)
print(zz.shape)
print(ii.shape)
print(jj.shape)