简体   繁体   English

使用 Numpy 将笛卡尔坐标转换为极坐标

[英]Converting Cartesian coordinates to polar coordinates with Numpy

I'm new to numpy, and I ran the following code我是 numpy 的新手,我运行了以下代码

C = np.random.random((10,2))
X = np.copy(C[:, 0])
Y = np.copy(C[:,1])
C[:, 0] = np.sqrt(X**2+Y**2)
C[:,1] = np.arctan2(Y,X)
print(C)

X,Y = C[:,0], C[:,1]
R = np.sqrt(X**2+Y**2)
T = np.arctan2(Y,X)
print(R)
print(T)

They are 2 different ways of transforming a 10x2 matrix of cartesian coordinates to polar coordinates.它们是将 10x2 笛卡尔坐标矩阵转换为极坐标的 2 种不同方法。 The first way was my answer, the second one is the correct one.第一种方法是我的答案,第二种方法是正确的。 They give different outputs, and I don't understand why.他们给出不同的输出,我不明白为什么。 Can anyone help me?谁能帮我? Thank you谢谢

You modified the column values of C in您修改了 C 的列值

C[:, 0] = np.sqrt(X**2+Y**2)
C[:,1] = np.arctan2(Y,X)

Instead, do:相反,请执行以下操作:

import numpy as np
C = np.random.random((10,2))
X = np.copy(C[:, 0])
Y = np.copy(C[:,1])
R = np.sqrt(X**2+Y**2)
T = np.arctan2(Y,X)
print(R)
print(T)

X,Y = C[:,0], C[:,1]
R = np.sqrt(X**2+Y**2)
T = np.arctan2(Y,X)
print(R)
print(T)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM