在Python和Matlab中,我编写了生成矩阵的代码,并使用索引函数填充它.Python代码的执行时间大约是Matlab代码执行时间的20倍.具有相同结果的两个函数是用python编写的,bWay()基于这个答案
这是完整的Python代码:
import numpy as np
from timeit import timeit
height = 1080
width = 1920
heightCm = 30
distanceCm = 70
centerY = height / 2 - 0.5;
centerX = width / 2 - 0.5;
constPart = height * heightCm / distanceCm
def aWay():
M = np.empty([height, width], dtype=np.float64);
for y in xrange(height):
for x in xrange(width):
M[y, x] = np.arctan(pow((pow((centerX - x), 2) + pow((centerY - y), 2)), 0.5) / constPart)
def bWay():
M = …Run Code Online (Sandbox Code Playgroud)