title: Why is Matlab warning me that "preallocation not recommended"
body: <p>I was writing up a function for the Coursera Machine Learning Course by Andrew Ng when I ran into a warning in Matlab. I haven't written what the answer should be but the starting code is all here except for one line for explanation purposes. The question is not fishing for the answer to the problem but an explanation of Matlab's warning. </p>
  
  <p>The warning (not error) I get says:</p>
  
  <pre><code>Line 6: The variable 'g' appears to be preallocated but preallocation is not recommended here 
  </code></pre>
  
  <p>Here's the code</p>
  
  <pre><code>function g = sigmoid(z)
  %SIGMOID Compute sigmoid function
  %   g = SIGMOID(z) computes the sigmoid of z.
  
  % You need to return the following variables correctly 
  g = zeros(size(z));
  
  % ====================== YOUR CODE HERE ======================
  % Instructions: Compute the sigmoid of each value of z (z can be a matrix,
  %               vector or scalar).
  
  g = 1./z;
  
  % =============================================================
  
  end
  </code></pre>