Skip to main content
added 17 characters in body
Source Link
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;
Array[PadRight[aWith[{a1 = PadRight[a, n][[Abs[#1n]}, (Array[a1[[Abs[#1 - #2] + 1]] &, {n, n}] )]// MatrixForm

enter image description here

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[a, n][[Abs[#1 - #2] + 1]] &, {n, n}] // MatrixForm

enter image description here

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

added 262 characters in body
Source Link
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;
Array[PadRight[a, n][[Abs[#1 - #2] + 1]] &, {n, n}] // MatrixForm

enter image description here

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}]

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[a, n][[Abs[#1 - #2] + 1]] &, {n, n}] // MatrixForm

enter image description here

Source Link
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}]