<p>Cannot test right now, but np.unique(b, return_inverse=True)[1].reshape(2, -1) should do what you are after, I think.<br>
</p>
<div class="gmail_quote">On Feb 2, 2014 11:58 AM, "Mads Ipsen" <<a href="mailto:mads.ipsen@gmail.com">mads.ipsen@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
I have run into a potential 'for loop' bottleneck. Let me outline:<br>
<br>
The following array describes bonds (connections) in a benzene molecule<br>
<br>
b = [[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 7,<br>
8, 9, 10, 11],<br>
[5, 6, 1, 0, 2, 7, 3, 8, 1, 4, 9, 2, 10, 5, 3, 4, 11, 0, 0, 1,<br>
2, 3, 4, 5]]<br>
<br>
ie. bond 0 connects atoms 0 and 5, bond 1 connects atom 0 and 6, etc. In<br>
practical examples, the list can be much larger (N > 100.000 connections.<br>
<br>
Suppose atoms with indices a = [1,2,3,7,8] are deleted, then all bonds<br>
connecting those atoms must be deleted. I achieve this doing<br>
<br>
i_0 = numpy.in1d(b[0], a)<br>
i_1 = numpy.in1d(b[1], a)<br>
b_i = numpy.where(i_0 | i_1)[0]<br>
b = b[:,~(i_0 | i_1)]<br>
<br>
If you find this approach lacking, feel free to comment.<br>
<br>
This results in the following updated bond list<br>
<br>
b = [[0, 0, 4, 4, 5, 5, 5, 6, 10, 11]<br>
[5, 6, 10, 5, 4, 11, 0, 0, 4, 5]]<br>
<br>
This list is however not correct: Since atoms [1,2,3,7,8] have been<br>
deleted, the remaining atoms with indices larger than the deleted atoms<br>
must be decremented. I do this as follows:<br>
<br>
for i in a:<br>
b = numpy.where(b > i, bonds-1, bonds) (*)<br>
<br>
yielding the correct result<br>
<br>
b = [[0, 0, 1, 1, 2, 2, 2, 3, 5, 6],<br>
[2, 3, 5, 2, 1, 6, 0, 0, 1, 2]]<br>
<br>
The Python for loop in (*) may easily contain 50.000 iteration. Is there<br>
a smart way to utilize numpy functionality to avoid this?<br>
<br>
Thanks and best regards,<br>
<br>
Mads<br>
<br>
--<br>
+---------------------------------------------------------+<br>
| Mads Ipsen |<br>
+----------------------+----------------------------------+<br>
| Gåsebæksvej 7, 4. tv | phone: +45-29716388 |<br>
| DK-2500 Valby | email: <a href="mailto:mads.ipsen@gmail.com">mads.ipsen@gmail.com</a> |<br>
| Denmark | map : <a href="http://www.tinyurl.com/ns52fpa" target="_blank">www.tinyurl.com/ns52fpa</a> |<br>
+----------------------+----------------------------------+<br>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div>