Matrices such that $A^2=A$ and $B^2=B$ Let $A,B$ be two matrices of $M(n,\mathbb{R})$ such that $$A^2=A\quad\text{and}\quad B^2=B$$
Then $A$ and $B$ are similar if and only if $\operatorname{rk}A = \operatorname{rk}B$.
The first implication is pretty easy because the rank is an invariant under matrix similarity. But the second one is kind of baffling me. I thought of reasoning about linear mappings instead of matrices. My reasoning was, basically, that if we consider the matrix as a linear mapping with respect to the canonical basis ($T(v)$ for the matrix $A$, $L(v)$ for the matrix $B$) then we have $$T(T(v))=T(v)\quad\text{and}\quad L(L(v))=L(v)$$
for all $v \in V$. Then the mapping must be either the $0$ function or the identity function (if this was the case, then the rest of the demonstration would be easy). But I soon realised that equating the arguments of the function, in general, doesn't work. 
Thanks in advance for your help.

Berci basically gave me the solution, forgot to add it (thank you!). Here it is, to avoid leaving things unsolved:
For an idempotent matrix, we have $V=\operatorname{ker}T\oplus\operatorname{Im}T$. Now, if $v\in\mathbb{R^n}$, then $\exists w \in \mathbb{R^n}$ such that $T(w)=v$. Then $T(T(w))=T(v)$, but then $T(w)=T(v)=v$. So taking a base of $\operatorname{Im}A$ and a base of $\operatorname{ker}A$ and joining them we automatically have an eigenvector basis. The matrix of the linear mapping with respect to this basis will be a matrix with $0$ everywhere but in the diagonal, and in the diagonal as many $1$'s as the rank of the mapping and as many zeroes as the dimension of the kernel. So, if two idempotent matrixes have the same rank then they are similar to the same matrix we've just built, and then they're similar to each other. 
