[Numpy-discussion] MATLAB to Numpy
Robert Kern
robert.kern at gmail.com
Sat Oct 21 15:03:07 EDT 2017
On Sat, Oct 21, 2017 at 10:45 AM, Andrei Berceanu <berceanu at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20171021/7e8feb86/attachment.html>
More information about the NumPy-Discussion
mailing list