samedi 22 juillet 2017

How to read an index which runs over all the elements of a list?

I have a basic question here. I want to generate random positions, with a minimum distance between them. Nevertheless, the most I have achieve is a position list where the element "n" is at a larger distance than the element "n-1", but it does not care about all the previous "n-2,3,4,..." elements. This is my code so far:

X = np.linspace(200,1900) 
Y = np.linspace(200,1900)
Xin = np.random.choice(X,1)
Yin = np.random.choice(Y,1)
dmin = 1000
i = 1
a = [ ]
Xprev = [Xin]
Yprev = [Yin]
while (i<sample+1):
    Xal = np.random.choice(X,1) 
    Yal = np.random.choice(Y,1)
    if sqrt((Xal-Xprev[i-1])**2+(Yal-Yprev[i-1]**2) > dmin:
        Xfin = Xal
        Yfin = Yal
        Xprev.append(Xfin)
        Yprev.append(Yfin)
        a_s = zip(Xfin,Yfin)
        a.append(a_s)
        out = vstack(a)
        i = i+1
    else:

        i=i

I think what I need is to change the index of Xprev in the if statement, for an index that runs over all and each of the elements of the X,Yprev lists. Could you explain me how to do that? Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire