小编DMi*_*den的帖子

优化Python/Numpy中的数组元素移位

问题:

运行我编写的数据分析代码的行分析后,我发现大约70%的总运行时间集中在对两个不同的数组操作例程的调用上.我最终希望以实时方式分析数据,因此这里的任何优化都会有很大帮助.

矩阵表格

这两个函数采用左边的矩阵并将其带到右边的表格(反之亦然).

我感兴趣的矩阵目前通过N 2d numpy数组存储为N(其中N是偶数).

码:

我编写了以下代码来完成此任务:

# Shifts elements of a vector to the left by the given amount.
def Vec_shift_L(vec, shift=0):
    s = vec.size
    out = np.zeros(s, dtype=complex)
    out[:s-shift] = vec[shift:]
    out[s-shift:] = vec[:shift]
    return out

# Shifts elements of a vector to the right by the given amount.
def Vec_shift_R(vec,shift=0):
    s=vec.size
    out=np.zeros(s, dtype=complex)
    out[:shift] = vec[s-shift:]
    out[shift:] = vec[:s-shift]
    return out

# Shifts a matrix from the left form (above) to the right form.
def OP_Shift(Trace): …
Run Code Online (Sandbox Code Playgroud)

python arrays optimization numpy matrix

7
推荐指数
1
解决办法
320
查看次数

标签 统计

arrays ×1

matrix ×1

numpy ×1

optimization ×1

python ×1