title: R function to create band matrix
body: <p>I need to write two functions <code>toBand()</code> and <code>replaceBand()</code>:</p>
  
  <pre><code>test3 &lt;- toBand(test1,3)
  test4 &lt;- replaceBand(test1, toBand(test2,3))
  </code></pre>
  
  <p>to get the output below.</p>
  
  <p>The first returns a matrix of the diagonal and co-diagonal elements, using the lower triangle of X. The second replaces the corresponding elements in X from the data in the band matrix supplied. The function then returns the modified X.</p>
  
  <p>Are there any packages that I can use to do this? Any suggestions on how to do this?</p>
  
  <p>Thank you</p>
  
  <pre><code>&gt; test1
       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]
  [1,] "a11" "a21" "a31" "a41" "a51" "a61"
  [2,] "a21" "a22" "a32" "a42" "a52" "a62"
  [3,] "a31" "a32" "a33" "a43" "a53" "a63"
  [4,] "a41" "a42" "a43" "a44" "a54" "a64"
  [5,] "a51" "a52" "a53" "a54" "a55" "a65"
  [6,] "a61" "a62" "a63" "a64" "a65" "a66"
  &gt; test2
       [,1]    [,2]    [,3]    [,4]    [,5]    [,6]  
  [1,] "*a11*" "*a21*" "*a31*" "*a41*" "*a51*" "*a61*"
  [2,] "*a21*" "*a22*" "*a32*" "*a42*" "*a52*" "*a62*"
  [3,] "*a31*" "*a32*" "*a33*" "*a43*" "*a53*" "*a63*"
  [4,] "*a41*" "*a42*" "*a43*" "*a44*" "*a54*" "*a64*"
  [5,] "*a51*" "*a52*" "*a53*" "*a54*" "*a55*" "*a65*"
  [6,] "*a61*" "*a62*" "*a63*" "*a64*" "*a65*" "*a66*"
  &gt; test3
       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]
  [1,] "a11" "a22" "a33" "a44" "a55" "a66"
  [2,] "a21" "a32" "a43" "a54" "a65" NA  
  [3,] "a31" "a42" "a53" "a64" NA    NA  
  [4,] "a41" "a52" "a63" NA    NA    NA  
  &gt; test4
       [,1]    [,2]    [,3]    [,4]    [,5]    [,6]  
  [1,] "*a11*" "*a21*" "*a31*" "*a41*" "a51"   "a61" 
  [2,] "*a21*" "*a22*" "*a32*" "*a42*" "*a52*" "a62" 
  [3,] "*a31*" "*a32*" "*a33*" "*a43*" "*a53*" "*a63*"
  [4,] "*a41*" "*a42*" "*a43*" "*a44*" "*a54*" "*a64*"
  [5,] "a51"   "*a52*" "*a53*" "*a54*" "*a55*" "*a65*"
  [6,] "a61"   "a62"   "*a63*" "*a64*" "*a65*" "*a66*"
  </code></pre>