You can also use [`SparseArray`](https://reference.wolfram.com/language/ref/SparseArray.html) 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