对二维列表中的每一项进行平方

Squaring every single item in a 2D list

所以我有一大堆要点。 我已将这些点拆分为 x 坐标和 y 坐标,然后将它们进一步拆分为 1000 个一组。

x = [points_Cartesian[x: x + 1000, 0] for x in range(0, len(points_Cartesian), 1000)]

(y 坐标看起来相同,但用 y 代替 x。)

我正在尝试将笛卡尔点转换为极坐标点,为此我必须对 x 中的每一项和 y 中的每一项进行平方。

for sublist1 in x:
   temp1 = []
   for inte1 in sublist1:
       temp1.append(inte1**2)
   xSqua.append(temp1)

之后,我将两个平方值相加并对它们求平方根以获得弧度。

rad = np.sqrt(xSqua + ySqua)

问题是,我从 10,000 点开始,在这段代码的某处它被削减到 1,000。 有谁知道错误是什么以及我如何解决它?

您已经在使用 numpy。您可以使用 numpy.reshape() 重塑矩阵,并使用 ** 运算符对整个数组按元素对整个数组进行平方,您的代码将比迭代快得多。

例如,假设我们有一个 10000x3 points_cartesian

points_Cartesian = np.random.random((10000,2))
# reshape to 1000 columns, as many rows as required
xpts = points_Cartesian[:, 0].reshape((-1, 1000)) 
ypts = points_Cartesian[:, 1].reshape((-1, 1000))

# elementwise square using **
rad = np.sqrt(xpts**2 + ypts**2)
ang = np.arctan2(ypts, xpts)

现在 radang10x1000 数组。