Skip to main content
3 of 3
added 17 characters in body
kale
  • 11k
  • 1
  • 34
  • 71

Without SparseArray:

n = 10;
Total[
 {DiagonalMatrix[Array[-1 &, n - 1], -1], 
  DiagonalMatrix[Array[2 &, n]], 
  DiagonalMatrix[Array[-1 &, n - 1], 1]}
 ]

Or strictly using Array:

Array[Which[#1 == #2, 2, Abs[#1 - #2] == 1, -1, True, 0] &, {10, 10}]

AND, what the heck? One more for more flexible applications:

a = {2, -1, -2, 3};
n = 10;
With[{a1 = PadRight[a, n]}, (Array[a1[[Abs[#1 - #2] + 1]] &, {n, n}])]//MatrixForm

enter image description here

kale
  • 11k
  • 1
  • 34
  • 71