Function working iteratively over each row in an individual column of an array - numpy
<p>I'm looking to change all the values in an array using the following formula:</p>

<pre><code>new_value = old_value * elec_space - elec_space
</code></pre>

<p>A complicating issue is that all values above 48 within the array will be increased by two, as 49 &amp; 50 will never exist in the original array (infile, shown below). This means that any value above 48 will have to have 2 subtracted from it before performing the above calculation.</p>

<p>Original values:</p>

<pre><code>elec_space = 0.5

infile = 
[[41,   42,   43,   44]
 [41,   42,   44,   45]
 [41,   43,   45,   47]
 [44,   45,   46,   47]
 [44,   45,   47,   48]
 [44,   46,   48,   52]
 [47,   48,   51,   52]
 [47,   48,   52,   53]
 [47,   51,   53,   55]]
</code></pre>

<p>Desired values:</p>

<pre><code>infile =
[[ 20,  20.5,   21,  21.5]
 [ 20,  20.5,  21.5,  22]
 [ 20    21,    22,   23]
 [21.5,  22,   22.5,  23]
 [21.5   22,    23,  23.5]
 [21.5, 22.5,  23.5, 24.5]
 [ 23,  23.5,   24,  24.5]
 [ 23,  23.5,  24.5,  25]
 [ 23,   24,    25,   26]]
</code></pre>

<p>I've tried:</p>

<pre><code>def remove_missing(infile):
    if infile &gt; 48:
        return (infile - 2) * elec_space - elec_space
    else:
        return infile * elec_space - elec_space
A = remove_missing(infile[:,0])
B = remove_missing(infile[:,1])
M = remove_missing(infile[:,2])
N = remove_missing(infile[:,3])
infile = np.column_stack((A, B, M, N))
</code></pre>

<p>And:</p>

<pre><code>def remove_missing(infile):
    return (infile - 2) * elec_space - elec_space if infile &gt; 50 else infile * elec_space - elec_space
A = remove_missing(infile[:,0])
B = remove_missing(infile[:,1])
M = remove_missing(infile[:,2])
N = remove_missing(infile[:,3])
</code></pre>

<p>But got the following traceback for each of them:</p>

<pre><code>---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-181-dcc8e29a527f&gt; in &lt;module&gt;()
      4     else:
      5         return infile * elec_space - elec_space
----&gt; 6 A = remove_missing(infile[:,0])
      7 B = remove_missing(infile[:,1])
      8 M = remove_missing(infile[:,2])

&lt;ipython-input-181-dcc8e29a527f&gt; in remove_missing(infile)
      1 def remove_missing(infile):
----&gt; 2     if infile &gt; 48:
      3         return (infile - 2) * elec_space - elec_space
      4     else:
      5         return infile * elec_space - elec_space

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 




---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-180-c407ec4fa95d&gt; in &lt;module&gt;()
      2     return (infile - 2) * elec_space - elec_space if infile &gt; 50 else infile * elec_space - elec_space
      3 
----&gt; 4 A = remove_missing(infile[:,0])
      5 B = remove_missing(infile[:,1])
      6 M = remove_missing(infile[:,2])

&lt;ipython-input-180-c407ec4fa95d&gt; in remove_missing(infile)
      1 def remove_missing(infile):
----&gt; 2     return (infile - 2) * elec_space - elec_space if infile &gt; 50 else infile * elec_space - elec_space
      3 
      4 A = remove_missing(infile[:,0])
      5 B = remove_missing(infile[:,1])

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
</code></pre>

<p>I don't think a.any or a.all are the right options, as I want the function to run iteratively for each row in the column of the array, not to alter all values based on one of the values being over 48.</p>

<p>Has anyone got any pointers on how best to tackle this, please?</p>

<p>One alternative approach could be to subtract <code>1</code> for elements that were greater than <code>48</code> from the result, like so -</p>

<pre><code>(infile - 2*(infile&gt;48))* elec_space - elec_space
</code></pre>

python arrays function numpy boolean-operations