[Python] Use numpy meshgrid for only 2 out of 3 variables

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

  1. Stack

    Stack Membro Participativo

    I have the following variables:

    centers = np.array([(2.78568, 0.0000), (4.32842, -0.775169), (7.43238, -1.000000)])
    ts = np.array([0.1, 0.2, 0.3])


    and I want a meshgrid where each of the tuples in centers are always paired together, but they are meshed with ts. i.e., I want the following output:

    sigma tau t
    2.78568 0.000000 0.1
    4.32842 -0.775169 0.1
    7.43238 -1.000000 0.1
    2.78568 0.000000 0.2
    4.32842 -0.775169 0.2
    7.43238 -1.000000 0.2
    2.78568 0.000000 0.3
    4.32842 -0.775169 0.3
    7.43238 -1.000000 0.3

    I tried this but there has to be a better way to do it?

    sigmas, taus = centers[:,0], centers[:,1]
    _inputs = np.array(np.meshgrid(taus, ts)).reshape(2,9).T
    inputs = np.append(np.zeros((9,1)), _inputs, 1)
    for input in inputs:
    center = centers[np.where(centers[:,1]==input[1])]
    input[0:2] = center


    I've tried a few combinations of arguments to meshgrid but haven't achieved what I want. Can someone help?

    Continue reading...

Compartilhe esta Página