title: How to write indicator function in matlab
body: <p>I am a new user of matlab and I want to tackle the following problem:</p>
  
  <p>I want to construct a piecewise constant function <code>f</code>. <code>f</code> should be an anonymous function like <code>f=@(t)1[0,0.25)(t)</code>. However, the number of intervals for the piecewise constant function is not fixed in general. Instead, the piecewise interval depends on users input. </p>
  
  <p>For example, if one enters 4, the piecewise interval becomes </p>
  
  <blockquote>
    <p>[0,0.25), [0.25,0.5), [0.5,0.75) and [0.75,1)</p>
  </blockquote>
  
  <p>then</p>
  
  <blockquote>
    <p>f=@(t)a1*1[0,0.25)(t)+a2*[0.25,0.5)(t)+a3*1[0.5,0.75)(t)+a4*1[0.75,1)(t); </p>
  </blockquote>
  
  <p>While if one enters 5, the piecewise interval becomes </p>
  
  <blockquote>
    <p>[0,0.2), [0.2,0.4), [0.4,0.6), [0.6,0.8) and [0.8,1)</p>
  </blockquote>
  
  <p>Are there any good ways to tackle the problem?</p>