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
Can your provide representative examples for tmp_p[x|y|z]? -paul
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?
Thank you so much, Best regards, Andrei _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
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
On 21 October 2017 at 21:03, Robert Kern robert.kern@gmail.com wrote:
Index with a boolean mask.
mask = (tmp_px > 2) px = tmp_px[mask] py = tmp_py[mask] # ... etc.
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]
David, that doesn’t work, because np.cumsum(mask)[mask] is always equal to np.arange(mask.sum()) + 1. Robert’s answer is correct.
Eric
On Sat, 21 Oct 2017 at 13:12 Daπid davidmenhur@gmail.com wrote:
On 21 October 2017 at 21:03, Robert Kern robert.kern@gmail.com wrote:
Index with a boolean mask.
mask = (tmp_px > 2) px = tmp_px[mask] py = tmp_py[mask] # ... etc.
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]
NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
On 21 October 2017 at 22:32, Eric Wieser wieser.eric+numpy@gmail.com wrote:
David, that doesn’t work, because np.cumsum(mask)[mask] is always equal to np.arange(mask.sum()) + 1. Robert’s answer is correct.
Of course, you are right. It makes sense in my head now.
Thank you so much, the solution was much simpler than I expected!
On Sat, 21 Oct 2017 23:04:43 +0200, Daπid davidmenhur@gmail.com wrote:
On 21 October 2017 at 22:32, Eric Wieser wieser.eric+numpy@gmail.com wrote:
David, that doesn’t work, because np.cumsum(mask)[mask] is always equal to np.arange(mask.sum()) + 1. Robert’s answer is correct.
Of course, you are right. It makes sense in my head now. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Hmm, so how come this doesn't work now?
mask = ((px > 2.) & ((py**2 + pz**2) / px**2 < 1.))
for arr in (px, py, pz, w, x, y, z): arr = arr[mask]
On Mon, 23 Oct 2017 15:05:26 +0200 (CEST), "Andrei Berceanu" berceanu@runbox.com wrote:
Thank you so much, the solution was much simpler than I expected!
On Sat, 21 Oct 2017 23:04:43 +0200, Daπid davidmenhur@gmail.com wrote:
On 21 October 2017 at 22:32, Eric Wieser wieser.eric+numpy@gmail.com wrote:
David, that doesn’t work, because np.cumsum(mask)[mask] is always equal to np.arange(mask.sum()) + 1. Robert’s answer is correct.
Of course, you are right. It makes sense in my head now. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
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:
Hmm, so how come this doesn't work now?
mask = ((px > 2.) & ((py**2 + pz**2) / px**2 < 1.))
for arr in (px, py, pz, w, x, y, z): arr = arr[mask]
On Mon, 23 Oct 2017 15:05:26 +0200 (CEST), "Andrei Berceanu" < berceanu@runbox.com> wrote:
Thank you so much, the solution was much simpler than I expected!
On Sat, 21 Oct 2017 23:04:43 +0200, Daπid davidmenhur@gmail.com wrote:
On 21 October 2017 at 22:32, Eric Wieser wieser.eric+numpy@gmail.com wrote:
David, that doesn’t work, because np.cumsum(mask)[mask] is always
equal
to np.arange(mask.sum()) + 1. Robert’s answer is correct.
Of course, you are right. It makes sense in my head now. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
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
So how can I mutate all of them at once?
On Fri, 27 Oct 2017 09:26:06 -0700, Robert Kern robert.kern@gmail.com wrote:
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 _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
One way would be ``` px, py, pz, w, x, y, z = [arr[mask] for arr in px, py, pz, w, x, y, z] ```
-- Marten