[Numpy-discussion] MATLAB to Numpy

Eric Wieser wieser.eric+numpy at gmail.com
Sat Oct 21 16:32:11 EDT 2017


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 at gmail.com> wrote:

On 21 October 2017 at 21:03, Robert Kern <robert.kern at 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 at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
>
​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20171021/73aa0fc9/attachment-0001.html>


More information about the NumPy-Discussion mailing list