首页 > 解决方案 > 对数组执行数学函数

问题描述

我是编程新手,所以我可能听起来很幼稚。我有一组数组

matrix([[0.23316744, 0.62686578, 0.23497639, 0.41566779, 0.18428155]]), matrix([[0.26199825, 0.7148431 , 0.2296318 , 0.36555626, 0.18962302]]),............. etc.

我想对每个元素执行 math.arctan 函数并将其恢复为相同的格式以执行进一步的操作。

我不能直接对其执行 math.arctan ,因为它给出了错误

TypeError: must be real number, not list

所以我尝试使用 np.asscalar 来分隔每个术语,但它不起作用并给出错误

TypeError: only size-1 arrays can be converted to Python scalars

在这方面的任何帮助都会非常有帮助。

标签: pythonarrays

解决方案


它给了你错误

TypeError: must be real number, not list

因为函数 Math.arctan Expect a number not a List ,一个简单的解决方案是循环到您的列表中,并为每个元素 L[i][j] 执行 L[i][j] = Math.arctan(L[i] [j])

在这段代码中,我想你有一个列表列表,每个子列表都包含数字

for i in range(0,len(l)):
    for j in range(0,len(l[i])):
        L[i][j] = Math.arctan(L[i][j])

推荐阅读