<s>[INST] Rank of the $n \times n$ matrix with ones on the main diagonal and $a$ off the main diagonal I want to find the rank of this $n\times n$ matrix
\begin{pmatrix}
1 & a & a & \cdots & \cdots & a \\
a & 1 & a & \cdots & \cdots & a \\
a & a & 1 & a & \cdots & a \\
\vdots & \vdots & a& \ddots & & \vdots\\
\vdots & \vdots & \vdots & & \ddots & \vdots \\
a & a & a & \cdots  &\cdots & 1
\end{pmatrix}
that is, the matrix whose diagonals are $1's$ and $a$ otherwise, where $a$ is any real number.
My first observation is when $a=0$ the rank is $n$ and when $a=1$ the rank is $1.$ Then I can assume $a\neq 0, 1$ and proceed row reduction to find its pivot rows. I obtain
\begin{pmatrix}
1 & a & a & \cdots  & a \\
0 & 1+a & a & \cdots & a \\
0 & a & 1+a & \cdots & a \\
\vdots & \vdots & \vdots & \ddots & \vdots \\
0 & a & a & \cdots  & 1+a
\end{pmatrix}
by subtracting the first row multiplied $a$ for each row below the first, and then divides the factor $(1-a)$, 
and stuck there. Any hints/helps?
 [/INST] Let
$$\mathrm M_n (a) := \begin{bmatrix} 1 & a & a & \dots & a & a\\ a & 1 & a & \dots & a & a\\ a & a & 1 & \dots & a & a\\ \vdots & \vdots & \vdots & \ddots & \vdots & \vdots\\ a & a & a & \dots & 1 & a\\ a & a & a & \dots & a & 1\end{bmatrix} = (1-a) \mathrm I_n + a 1_n 1_n^{\top}$$
The eigenvalues of rank-$1$ matrix $a 1_n 1_n^{\top}$ are 


*

*$\color{blue}{0}$ with multiplicity $n-1$.

*$\color{blue}{n a}$ with multiplicity $1$. 


Thus, the eigenvalues of $\mathrm M_n (a) = (1-a) \mathrm I_n + a 1_n 1_n^{\top}$ are 


*

*$\color{blue}{1-a}$ with multiplicity $n-1$.

*$(1-a) + na = \color{blue}{(n-1) \, a + 1}$ with multiplicity $1$. 


We could also have arrived at this conclusion computing the characteristic polynomial of $\mathrm M_n (a)$
$$\begin{array}{rl} \det ( s \mathrm I_n - \mathrm M_n (a) ) &= \det \left( (s-(1-a)) \mathrm I_n - a 1_n 1_n^{\top} \right)\\ &= \det \left( (s-(1-a)) \left( \mathrm I_n - \frac{a}{s-(1-a)} 1_n 1_n^{\top} \right) \right)\\ &= (s-(1-a))^n \cdot \det \left( \mathrm I_n - \frac{a}{s-(1-a)} 1_n 1_n^{\top} \right)\\ &= (s-(1-a))^n \cdot \left(1 - \frac{n a}{s-(1-a)}\right)\\ &= (s-(1-a))^{n-1} \cdot \left(s-(1-a) - n a\right)\\ &= (s-(1-a))^{n-1} \cdot \left( s - ((n-1) \, a + 1) \right)\end{array}$$
where the matrix determinant lemma was used. If
$$a \in \left\{ -\frac{1}{n-1}, 1 \right\}$$ 
then $\mathrm M_n (a)$ is singular. Thus, using the rank-nullity theorem, we conclude that
$$\boxed{\mbox{rank} (\mathrm M_n (a)) = \begin{cases} 1 & \text{if } a = 1\\ n-1 & \text{if } a = -\frac{1}{n-1}\\ n & \text{otherwise}\end{cases}}$$
</s>