title: Convert matlab code into python for matrix creation
body: <p>I'm struggling to create the following matrix in python:</p>
  
  <pre><code>| 1 -2  1  0 ...  0  |
  | 0  1 -2  1 ... ... |
  |... ... ... ...  0  |
  | 0  ... 0  1 -2  1  |
  </code></pre>
  
  <p>I've the matlab code below which seems to create this matrix (<a href="https://bsamig.uef.fi/pdf/detrend.pdf" rel="nofollow">article</a>) but I cannot convert it in python code.</p>
  
  <p>Matlab code:</p>
  
  <pre><code>D2 = spdiags(ones(T-2,1)*[1 -2 1],[0:2],T-2,T);
  </code></pre>
  
  <p>T is the number of columns. </p>
  
  <p>The code in python looks like this:</p>
  
  <pre class="lang-py prettyprint-override"><code>from scipy.sparse import spdiags
  D2 = spdiags( (ones((T-2,1))*array([1,-2,1])),arange(0,3),T-2,T)
  </code></pre>
  
  <p>This latter produce the following error:</p>
  
  <blockquote>
    <p>ValueError: number of diagonals (327) does not match the number of
    offsets (3)</p>
  </blockquote>
  
  <p>But if I transpose the matrix like that:</p>
  
  <pre class="lang-js prettyprint-override"><code>D2 = spdiags( (ones((T-2,1))*array([1,-2,1])).T,arange(0,3),T-2,T)
  </code></pre>
  
  <p>I get the following result:</p>
  
  <pre><code>matrix([[ 1., -2.,  1., ...,  0.,  0.,  0.],
          [ 0.,  1., -2., ...,  0.,  0.,  0.],
          [ 0.,  0.,  1., ...,  0.,  0.,  0.],
          ..., 
          [ 0.,  0.,  0., ...,  1.,  0.,  0.],
          [ 0.,  0.,  0., ..., -2.,  0.,  0.],
          [ 0.,  0.,  0., ...,  1.,  0.,  0.]])
  </code></pre>
  
  <p>Does anybody can help me? Where am I wrong?</p>