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

Index with a boolean mask.

mask = (tmp_px > 2)
px = tmp_px[mask]
py = tmp_py[mask]
# ... etc.

--
Robert Kern