Skip to main content
deleted 13 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

You can also use SparseArray as follows

sa[n_] := SparseArray[{{1, _}|{_, 1}|{i_, j_i_} /; i > j1 -> 1 - i, {i_, i_j_} /; 1 < i >< 1j -> 1 - i0},
  {n, n}], 1]

matrix2 = sa[7] 
matrix2 == matrix

True

You can also use SparseArray as follows

sa[n_] := SparseArray[{{1, _}|{_, 1}|{i_, j_} /; i > j -> 1, {i_, i_} /; i > 1 -> 1 - i},
  {n, n}]

matrix2 = sa[7] 
matrix2 == matrix

True

You can also use SparseArray as follows

sa[n_] := SparseArray[{{i_, i_} /; i > 1 -> 1 - i, {i_, j_} /; 1 < i < j ->  0}, {n, n}, 1]

matrix2 = sa[7] 
matrix2 == matrix

True

Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

You can also use SparseArray as follows

sa[n_] := SparseArray[{{1, _}|{_, 1}|{i_, j_} /; i > j -> 1, {i_, i_} /; i > 1 -> 1 - i},
  {n, n}]

matrix2 = sa[7] 
matrix2 == matrix

True