<div dir="ltr">On Sat, Oct 21, 2017 at 10:45 AM, Andrei Berceanu <<a href="mailto:berceanu@runbox.com">berceanu@runbox.com</a>> wrote:<br>><br>> Hi,<br>><br>> I am new to Numpy, and would like to start by translating a (badly written?) piece of MATLAB code.<br>> What I have come up with so far is this:<br>><br>> px = np.zeros_like(tmp_px); py = np.zeros_like(tmp_py); pz = np.zeros_like(tmp_pz)<br>> w = np.zeros_like(tmp_w)<br>> x = np.zeros_like(tmp_x); y = np.zeros_like(tmp_y); z = np.zeros_like(tmp_z)<br>><br>> j=-1<br>> for i in range(tmp_px.size):<br>>     if tmp_px[i] > 2:<br>>         j += 1<br>>         px[j] = tmp_px[i]<br>>         py[j] = tmp_py[i]<br>>         pz[j] = tmp_pz[i]<br>>         w[j] = tmp_w[i]<br>>         x[j] = tmp_x[i]<br>>         y[j] = tmp_y[i]<br>>         z[j] = tmp_z[i]<br>><br>> px=px[:j+1]; py=py[:j+1]; pz=pz[:j+1]<br>> w=w[:j+1]<br>> x=x[:j+1]; y=y[:j+1]; z=z[:j+1]<br>><br>> It works, but I'm sure it's probably the most inefficient way of doing it. What would be a decent rewrite?<br><br>Index with a boolean mask.<div><br></div><div>mask = (tmp_px > 2)</div><div>px = tmp_px[mask]</div><div>py = tmp_py[mask]</div><div># ... etc.<br><br>--<br>Robert Kern</div></div>