我有多个矩阵,我想在我之前创建的矩阵的基础上创建另一个“g”。我有一个生成‘g’矩阵的一般公式,但是,我想根据矩阵‘θ’来修改其中的一些公式。如果“theta”中的一个元素有零值,我想得到该元素的位置,并找到在“g”中具有相同位置的元素来应用第二个公式
目前,我有以下代码。但问题是它运行得很慢。我必须生成多个类似的矩阵,我想知道是否有人知道一个更快的方法?提前谢谢
import numpy as np
np.seterr(divide='ignore', invalid='ignore')
x = np.linspace(-100.0, 100.0, 401)
y = np.linspace(100.0, -100.0, 401)
xx, yy = np.meshgrid(x, y)
xxx = xx / 10
yyy = yy / 10
r = np.sqrt((xxx ** 2.0) + (yyy ** 2.0))
theta = np.degrees(np.arctan(xxx / yyy))
m = 1.5
uv = (xxx * xxx) + ((yyy - (m / 2)) * (yyy + (m / 2)))
umag = np.sqrt((xxx ** 2) + ((yyy - (m / 2)) ** 2))
vmag = np.sqrt((xxx ** 2) + ((yyy + (m / 2)) ** 2))
theta2 = np.arccos(uv / (umag * vmag))
g = np.absolute(theta2 * 1000 / (m * xxx))
l = len(g)
for a in range(l):
for b in range(len(g[a])):
if (theta[a][b] == 0):
g[a][b] = 1 * 1000 / ((r[a][b]**2) - ((m**2) / 4))
print(g)
else:
pass
好吧,结果很好。我把for循环改成了这个:
现在它运行得快得多,所有元素的检查都被取消了。代码现在只检查满足条件的地方。 非常感谢
您可以使用
np.where(theta == 0)
。它返回元组有关详细信息,请查看this
相关问题 更多 >
编程相关推荐