title: Can anyone tell me how to write indicator function for vector input in matlab?
body: <p>I am a new user of matlab and stackoverflow. I asked a <a href="https://stackoverflow.com/questions/37349914/how-to-write-indicator-function-in-matlab/37350742#37350742">question</a> about how to write indicator function f, the question was as follow:</p>
  
  <p>"f should be an anonymous function like f=@(t)1[0,0.25)(t). 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>I am glad that some users answered my question. 
  The <a href="https://stackoverflow.com/a/37350742/2955997">solution</a> answered is as follow:</p>
  
  <pre><code>%defines weight vector. for example: a1=1, a2=2, a3=3, a4=4,a5=5
  A = 1:5;  
  %defines a range vector
  ranges = [0:(1/length(A)):1,inf];
  
  %The padding is for handling cases where t&lt;0 or t&gt;=1
  
  APadded = [0,A,0]; 
  f=@*(t)APadded(find(t &lt; ranges,1,'first'));
  
  Result
  f(0.1) = 1, f(0.3) = 2, f(0.5) = 3, f(0.7) = 4, f(0.9) = 5, f(-0.1) = f(1.1) = 0;
  </code></pre>
  
  <p>I want to extend the codes a little bit. For example, if I define @(X)sun(X), when the input of X is a vector, it gives output to be a vector. However, the above solution for indicator result does not. One can try f([0.1 0.3 0.5 0.7 0.9]) which does not give 1, 2, 3, 4, 5. </p>
  
  <p>How to solve this problem so that even if the input is a vector, it will give correct result? </p>