![](https://secure.gravatar.com/avatar/561acf6968bdd9bcbc6cb71731539b66.jpg?s=120&d=mm&r=g)
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? Thank you so much, Best regards, Andrei
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
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.
Index with a boolean mask. mask = (tmp_px > 2) px = tmp_px[mask] py = tmp_py[mask] # ... etc. -- Robert Kern
![](https://secure.gravatar.com/avatar/18a30ce6d84de6ce5c11ce006d10f616.jpg?s=120&d=mm&r=g)
On 21 October 2017 at 21:03, Robert Kern <robert.kern@gmail.com> wrote:
That isn't equivalent, note that j only increases when tmp_px > 2. I think you can do it with something like: mask = tmp_px > 2 j_values = np.cumsum(mask)[mask] i_values = np.arange(len(j_values)) px[i_values] = tmp_i[j_values]
![](https://secure.gravatar.com/avatar/697900d3a29858ea20cc109a2aee0af6.jpg?s=120&d=mm&r=g)
In what way does it not work? Does it error out at the `arr = arr[mask]` step? Or is it that something unexpected happens? I am guessing that you are trying to mutate the px, py, pz, w, x, y, z arrays? If so, that for-loop won't do it. In python, a plain simple assignment merely makes the variable point to a different object. It doesn't mutate the object itself. Cheers! Ben Root On Fri, Oct 27, 2017 at 11:43 AM, Andrei Berceanu <berceanu@runbox.com> wrote:
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
On Fri, Oct 27, 2017 at 9:16 AM, Benjamin Root <ben.v.root@gmail.com> wrote:
In what way does it not work? Does it error out at the `arr = arr[mask]`
step? Or is it that something unexpected happens?
I am guessing that you are trying to mutate the px, py, pz, w, x, y, z
arrays? If so, that for-loop won't do it. In python, a plain simple assignment merely makes the variable point to a different object. It doesn't mutate the object itself. More specifically, it makes the name on the left-hand side point to the object that's evaluated by the right-hand side. So this for loop is just re-assigning objects to the name "arr". The names "px", "py", etc. are not being reassigned. Here is a good article on how Python assignment works: https://nedbatchelder.com/text/names.html -- Robert Kern
![](https://secure.gravatar.com/avatar/851ff10fbb1363b7d6111ac60194cc1c.jpg?s=120&d=mm&r=g)
One way would be ``` px, py, pz, w, x, y, z = [arr[mask] for arr in px, py, pz, w, x, y, z] ``` -- Marten
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
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.
Index with a boolean mask. mask = (tmp_px > 2) px = tmp_px[mask] py = tmp_py[mask] # ... etc. -- Robert Kern
![](https://secure.gravatar.com/avatar/18a30ce6d84de6ce5c11ce006d10f616.jpg?s=120&d=mm&r=g)
On 21 October 2017 at 21:03, Robert Kern <robert.kern@gmail.com> wrote:
That isn't equivalent, note that j only increases when tmp_px > 2. I think you can do it with something like: mask = tmp_px > 2 j_values = np.cumsum(mask)[mask] i_values = np.arange(len(j_values)) px[i_values] = tmp_i[j_values]
![](https://secure.gravatar.com/avatar/697900d3a29858ea20cc109a2aee0af6.jpg?s=120&d=mm&r=g)
In what way does it not work? Does it error out at the `arr = arr[mask]` step? Or is it that something unexpected happens? I am guessing that you are trying to mutate the px, py, pz, w, x, y, z arrays? If so, that for-loop won't do it. In python, a plain simple assignment merely makes the variable point to a different object. It doesn't mutate the object itself. Cheers! Ben Root On Fri, Oct 27, 2017 at 11:43 AM, Andrei Berceanu <berceanu@runbox.com> wrote:
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
On Fri, Oct 27, 2017 at 9:16 AM, Benjamin Root <ben.v.root@gmail.com> wrote:
In what way does it not work? Does it error out at the `arr = arr[mask]`
step? Or is it that something unexpected happens?
I am guessing that you are trying to mutate the px, py, pz, w, x, y, z
arrays? If so, that for-loop won't do it. In python, a plain simple assignment merely makes the variable point to a different object. It doesn't mutate the object itself. More specifically, it makes the name on the left-hand side point to the object that's evaluated by the right-hand side. So this for loop is just re-assigning objects to the name "arr". The names "px", "py", etc. are not being reassigned. Here is a good article on how Python assignment works: https://nedbatchelder.com/text/names.html -- Robert Kern
![](https://secure.gravatar.com/avatar/851ff10fbb1363b7d6111ac60194cc1c.jpg?s=120&d=mm&r=g)
One way would be ``` px, py, pz, w, x, y, z = [arr[mask] for arr in px, py, pz, w, x, y, z] ``` -- Marten
participants (7)
-
Andrei Berceanu
-
Benjamin Root
-
Daπid
-
Eric Wieser
-
Marten van Kerkwijk
-
Paul Hobson
-
Robert Kern