Let $A$ be an $n \times n$ diagonal matrix with characteristic polynomial $(x-a)^p(x-b)^q$,where $a$ and $b$ are distinct real numbers. Let $V$ be the real vector space of all $n \times n$ matrices $B$ such that $AB=BA$. Determine the dimension of $V$.
I am getting the answer as $p^2+q^2$.
I first wrote the matrix $A$ as a diagonal matrix with the first $p$ entries along the diagonal as $a$ and the next $q$ entries $b$. Then carry out the multiplication $AB$ and $BA$ and match the entries $(1,1)$ to $(n,n)$. We see that only the $p^2 + q^2$ entries match, the rest must be $0$. Hence, the dimension should be $p^2 + q^2$. Is this the correct answer.