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;
Array[PadRight[aWith[{a1 = PadRight[a, n][[Abs[#1n]}, (Array[a1[[Abs[#1 - #2] + 1]] &, {n, n}] )]// MatrixForm