title: Vectorize loop that creates tridiagonal matrix in Matlab
body: <p>I have a for-loop that creates a tridiagonal matrix like so:</p>
  
  <pre><code>m = 5;
  Fo = 0.35;
  
  A(1,1) = 1+2*Fo;
  A(1,2) = 1+Fo;
  
  for i = 2:m-1
      A(i,i-1) = 1;
      A(i,i) = 2;
      A(i,i+1) = 3;
  end
  
  A(m,m-1) = 4;
  A(m,m) = 5;
  </code></pre>
  
  <p>where the output is:</p>
  
  <pre><code>A =
      1.7000    1.3500         0         0         0
      1.0000    2.0000    3.0000         0         0
           0    1.0000    2.0000    3.0000         0
           0         0    1.0000    2.0000    3.0000
           0         0         0    4.0000    5.0000
  </code></pre>
  
  <p>I am trying to vectorize the creation of the tridiagonal matrix using the following to replace the for-loop:</p>
  
  <pre><code>i = 2:m-1;
  A(i,i-1) = 1;
  A(i,i) = 2;
  A(i,i+1) = 3;
  </code></pre>
  
  <p>Unfortunately the the output is not correct:</p>
  
  <pre><code>A =
      1.7000    1.3500         0         0         0
      1.0000    2.0000    3.0000    3.0000    3.0000
      1.0000    2.0000    3.0000    3.0000    3.0000
      1.0000    2.0000    3.0000    3.0000    3.0000
           0         0         0    4.0000    5.0000
  </code></pre>
  
  <p>Is it possible to create such a matrix using vectorization instead of a for-loop? I will eventually need to create a much larger and more complicated tridiagonal matrix thus hoping to use vectorization to speed up the process.</p>