[Python] Increase speed of two for loops in Python

Discussão em 'Python' iniciado por Stack, Setembro 15, 2024.

  1. Stack

    Stack Membro Participativo

    I have a double for loop in a Python code. I want to increase the execution speed, but the matrix is built using data from the previous row and column. How can I achieve this?

    import numpy as np

    num_rows, num_cols = 100, 5

    matrix = np.zeros((num_rows, num_cols))

    matrix[0, :] = np.random.rand(num_cols)
    matrix[:, 0] = np.random.rand(num_rows)

    coeff1 = np.random.rand(num_rows)
    coeff2 = np.random.rand(num_rows)
    coeff3 = np.random.rand(num_rows)

    result = np.zeros_like(matrix)

    for j in range(1, num_cols):
    for n in range(1, num_rows):

    term1 = coeff1[n] * matrix[n-1, j-1]
    term2 = coeff2[n] * matrix[n, j-1]
    term3 = coeff3[n] * matrix[n-1, j]

    result[n, j] = term1 + term2 + term3

    Continue reading...

Compartilhe esta Página