Note that there are some explanatory texts on larger screens.

plurals
  1. POCode works but too slow
    primarykey
    data
    text
    <p>How do i make this next piece of code run faster? I calculate the distance between a number of points first (no problem), but after that, i need to get the mean of the values all the points in one list that are closer than (in this case 20m), and if that 20 is small, this piece of code is fast, but otherwise, it is very slow, since i need the indices etc- The next piece of code does exactly what i want, but it is extremely slow if i take 20 for value instead of for example 6 (because for 20, there are about 100 points close enough, while for 6, there are only 3 or 5 or so)</p> <pre><code>D = numpy.sqrt((xf[:,None] - xg[None,:])**2 + (yf[:,None] - yg[None,:])**2 + (zf[:,None] - zg[None,:])**2) dumdic = {} l1=[] for i in range(len(xf)): dumdic[i] = D[i,:][D[i,:]&lt;20] # gets the values where the distance is small enough A=[] for j in range(len(dumdic[i])): A.append(G.epsilon[list(D[i,:]).index(dumdic[i][j])]) # for each point in that dummy dictionary, gets the index where i need to take the epsilon value, and than adds that right epsilon value to A l1.append(numpy.mean(numpy.array(A))) a1 = numpy.array(l1) </code></pre> <p>G.epsilon is the array in which for each point we have a measurement value. So in that array i need to take (for each point in the other array) the mean for all points in this array that are close enough to that other point.</p> <p>If you need more details, just ask</p> <p>after the reply of @gregwittier, this is the better version: can anyone oneliner it yet? (twoliner, since D=... takes one line) would be more pythonic i guess if i dont have the l1=... and the recasting to numpy array, but the worst thing now is to kill that for-loop, by using an axis argument or so?</p> <pre><code>D = numpy.sqrt((xf[:,None] - xg[None,:])**2 + (yf[:,None] - yg[None,:])**2 + (zf[:,None] - zg[None,:])**2) l1=[] for i in range(len(xf)): l1.append(numpy.mean(G.epsilon[D[i,:]&lt;20])) a1 = numpy.array(l1) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload