NxN diagonal matrix with mixed zero and nonzero diagonal elements
Another solution:
diagonalMatrix[n_] := DiagonalMatrix@PadRight[{}, n, {0, 1}]
n = 9;
ReplacePart[ConstantArray[0, {n, n}], {i_?EvenQ, i_?EvenQ} -> 1]
% // MatrixForm
Or
Clear["`*"];
n = 9;
M = SparseArray[{{i_, i_} /; Mod[i, 2] == 0 -> 1}, {n, n}];
M // Normal // MatrixForm
Mod[DiagonalMatrix[Range[#] - 1], 2] & @ 7
Boole @ Array[EvenQ@# && # == #2 &, {#, #}] & @ 7
SparseArray[Band[{2, 2}, {#, #}, {2, 2}] -> 1] & @ 7
SparseArray[{i_, i_} :> Mod[i + 1, 2], {#, #}] & @ 7
MapAt[0 # &, IdentityMatrix@#, {;; , ;; ;; 2}] & @ 7
MapIndexed[Mod[#2[[1]] + 1, 2] # &]@ IdentityMatrix[#] & @ 7
ReplacePart[IdentityMatrix@#, {i_, i_} :> Mod[i + 1, 2]] & @ 7
all give
% // MatrixForm // TeXForm
$\left( \begin{array}{ccccccc} 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$