Skip to main content
1 of 3
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}]
kale
  • 11k
  • 1
  • 34
  • 71