python, plot triangular function into 2d arrays
<p>I'm new to python, in this case, I want to put my function into 2d arrays, so I can plot the function. Here is my triangle function, I'm using it for fuzzy logic:</p>

<pre><code>def triangle (z,a,b,c):
    if (z&lt;=a) | (z&gt;=c):
        y = 0
    elif (a&lt;=z) &amp; (z&lt;=b):
        y = (z-a) / (b-a)
    elif (b&lt;=z) &amp; (z&lt;=c):
        y = (b-z) / (c-b)
    return y
</code></pre>

<p>and I'm trying to make the array using numpy, 
<code>np.linspace</code> but I can't get it done,
I've tried to use the fuzzy library, but nothing works.</p>

<p>Looks like <code>a, b, c</code> are constants and <code>z</code> is a <code>np.linspace</code> between <code>a</code>, and <code>c</code>.</p>

<p>You can make use of <a href="http://docs.scipy.org/doc/numpy/user/basics.indexing.html#boolean-or-mask-index-arrays" rel="nofollow">Boolean Indexing</a> , <a href="https://scipy.github.io/old-wiki/pages/Cookbook/Indexing#Boolean_indexing" rel="nofollow">SciPy cookbook/Indexing</a></p>

<pre><code>a = 1
b = 2
c = 3

def triangle (z, a = a, b = b, c = c):
    y = np.zeros(z.shape)
    y[z &lt;= a] = 0
    y[z &gt;= c] = 0
    first_half = np.logical_and(a &lt; z, z &lt;= b)
    y[first_half] = (z[first_half]-a) / (b-a)
    second_half = np.logical_and(b &lt; z, z &lt; c)
    y[second_half] = (c-z[second_half]) / (c-b)
    return y

z = np.linspace(a, c, num = 51)
y = triangle(z, a, b, c)

q = np.vstack((z, y)) # shape = (2, 50) ... [[z, z, z, ...], [y, y, y, ...]]
q =  q.T # shape = (50, 2) ... [[z, y], [z, y], ....]
</code></pre>

<p><a href="http://i.stack.imgur.com/X3sNY.png" rel="nofollow"><img src="http://i.stack.imgur.com/X3sNY.png" alt="enter image description here"></a></p>

<hr>

<p>When you use a numpy ndarray in a comparison expression the result is a boolean array:</p>

<pre><code>&gt;&gt;&gt; q = np.linspace(0, 20, num = 50)
&gt;&gt;&gt; print(q)
[  0.           0.40816327   0.81632653   1.2244898    1.63265306
   2.04081633   2.44897959   2.85714286   3.26530612   3.67346939
   4.08163265   4.48979592   4.89795918   5.30612245   5.71428571
   6.12244898   6.53061224   6.93877551   7.34693878   7.75510204
   8.16326531   8.57142857   8.97959184   9.3877551    9.79591837
  10.20408163  10.6122449   11.02040816  11.42857143  11.83673469
  12.24489796  12.65306122  13.06122449  13.46938776  13.87755102
  14.28571429  14.69387755  15.10204082  15.51020408  15.91836735
  16.32653061  16.73469388  17.14285714  17.55102041  17.95918367
  18.36734694  18.7755102   19.18367347  19.59183673  20.        ]
&gt;&gt;&gt; print(q &lt; 5)
[ True  True  True  True  True  True  True  True  True  True  True  True
  True False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False]
&gt;&gt;&gt; print(q &gt; 15)
[False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False  True  True  True  True  True  True  True  True  True  True  True
  True  True]
&gt;&gt;&gt; print(np.logical_and(q &gt; 5, q &lt; 15))
[False False False False False False False False False False False False
 False  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True  True  True  True  True  True  True  True  True
  True False False False False False False False False False False False
 False False]
&gt;&gt;&gt; 
</code></pre>

<p>You can use a boolean array to <em>select</em> portions of an array that meet your condition:</p>

<pre><code>&gt;&gt;&gt; q[np.logical_and(q &gt; 7, q &lt; 11)]
array([  7.34693878,   7.75510204,   8.16326531,   8.57142857,
         8.97959184,   9.3877551 ,   9.79591837,  10.20408163,  10.6122449 ])
&gt;&gt;&gt; 
</code></pre>

<p>When you use boolean indexing in an assignment statement the <em>right-hand-side</em> is only assigned to the indices where the comparison is <code>True</code>:</p>

<pre><code>&gt;&gt;&gt; q[np.logical_and(q &gt; 7, q &lt; 11)] = -1
&gt;&gt;&gt; print(q)
[  0.           0.40816327   0.81632653   1.2244898    1.63265306
   2.04081633   2.44897959   2.85714286   3.26530612   3.67346939
   4.08163265   4.48979592   4.89795918   5.30612245   5.71428571
   6.12244898   6.53061224   6.93877551  -1.          -1.          -1.          -1.
  -1.          -1.          -1.          -1.          -1.          11.02040816
  11.42857143  11.83673469  12.24489796  12.65306122  13.06122449
  13.46938776  13.87755102  14.28571429  14.69387755  15.10204082
  15.51020408  15.91836735  16.32653061  16.73469388  17.14285714
  17.55102041  17.95918367  18.36734694  18.7755102   19.18367347
  19.59183673  20.        ]
&gt;&gt;&gt; 
</code></pre>

python arrays numpy triangular