How to calculate the degrees of freedom of an $r$-ranked matrix with the size being $n\times n$? Treat matrices as vectors lying in $\mathbb{R}^{n^2}$. It can be imagined matrices with rank $r (r<n)$ are supposed to lie within a manifold of lower dimension. For example, the singular matrices lie within a $(n^2−1)$-dimensional manifold, because they satisfy $\det(M)=0$, the sole constraint. Typically, the number of independent constraints is equal to the difference between those two dimensions.
Alternatively, we call the dimension of that lower-dimensional manifold as "degrees of freedom". 
Now how do we calculate the degrees of freedom of an $r$-ranked matrix with size $n\times n$?
The answer says the degrees of freedom is $n^2-(n-r)^2=(2n-r)r$. I try to interpret it as follows:
First, by elementary matrices, every matrix $M$ with the rank of $r$ can be transformed into
$$M\sim\begin{pmatrix}
I_r&0\\
0&0
\end{pmatrix}_{n\times n}$$
Now the constraints come from the block at bottom right, where the entries are suppressed to be zero. So there are $(n-r)^2$ constraints, which lead to the answer (why the number of constraints do not agree with the number of zeroes is because they are not all independent, intuitively).
The explanation is not formal at all. Can anyone provide a refined version? Thank you~
EDIT: Provide further explanation of "degrees of freedom"
 A: Generically, the following procedure can be used to construct an $n\times n$ matrix with rank $r$:


*

*Choose the first $r$ columns of the matrix, with $n$ degrees of freedom for each column.  Generically, the results will be linearly independent.

*We can now choose the remaining columns to be linear combinations of the first $r$ columns.  This gives $r$ degrees of freedom for each column, namely the $r$ coefficients of the linear combinations.
Thus, the dimension of the space of rank-$r$ matrices is
$$
rn + (n-r)r\text{,}
$$
which is the same as $n^2 - (n-r)^2$.
A: I may use Singular Value Decomposition.
Any n by n matrix $M$ of rank r can be decomposed into $M=\sum_{k=1}^r\sigma_ku_kv_k^*$, where the $u_k$'s and $v_k$'s are the left and right singular vectors, and the $\sigma_k$'s are the singular values. Then we can compute the degree of freedom by counting it respectively for $\sigma_k,\ u_k,\ and \ v_k$, and then sum them up.

*

*For $\sigma_k$'s, it can be chosen freely in $R-\{0\}$, so the degree of freedom for this part is r.

*For $u_k$'s, we can first choose them freely in unit circle of the complex plane, and then let them satisfy the constraints of unit length and orthogonality. This results in degree of freedom of $ nr - r - C_r^2$, because we have $r$ unit length constraints and $C_r^2$ orthogonality constraints.

*We then sum them up, and will get $(2n-r)r$.

