Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I prefer to use <strong><em>NP.where</em></strong> for indexing tasks of this sort (rather than <em>NP.ix_</em>)</p> <p>What is not mentioned in the OP is whether the result is selected by location (row/col in the source array) or by some condition (e.g., m >= 5). In any event, the code snippet below covers both scenarios.</p> <p>Three steps: </p> <ol> <li><p>create the <strong><em>condition array</em></strong>;</p></li> <li><p>generate an <strong><em>index array</em></strong> by calling <em>NP.where</em>, passing in this condition array; and</p></li> <li><p><em>apply</em> this index array against the source array</p></li> </ol> <hr> <pre><code>&gt;&gt;&gt; import numpy as NP &gt;&gt;&gt; cnd = (m==1) | (m==5) | (m==7) | (m==6) &gt;&gt;&gt; cnd matrix([[ True, False], [False, True], [ True, False], [ True, False]], dtype=bool) &gt;&gt;&gt; # generate the index array/matrix &gt;&gt;&gt; # by calling NP.where, passing in the condition (cnd) &gt;&gt;&gt; ndx = NP.where(cnd) &gt;&gt;&gt; ndx (matrix([[0, 1, 2, 3]]), matrix([[0, 1, 0, 0]])) &gt;&gt;&gt; # now apply it against the source array &gt;&gt;&gt; m[ndx] matrix([[1, 5, 7, 6]]) </code></pre> <p><br/> The argument passed to NP.where, <em>cnd</em>, is a boolean array, which in this case, is the result from a single expression comprised of compound conditional expressions (first line above)</p> <p>If constructing such a value filter doesn't apply to your particular use case, that's fine, you just need to generate the actual boolean matrix (the value of <strong><em>cnd</em></strong>) some other way (or create it directly).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload