Definition of a matrix by matching two vectors
[The] matrix, which contains like the identity matrix in the diagonal ones, but only if the values match, otherwise zero.
is a very mathematical description. Don't confuse lack of formulas for lack of mathematics. Personally, I would maybe write it a little differently, but that's mostly just to remove any potential for misinterpretation. For instance, saying that the matrix is diagonal, and explicitly stating that the non-zero entries are $1$ is a bit clearer than saying "contains like the identity matrix". At least to people who are used to reading mathematical texts.
Here is my suggestion:
The diagonal $n\times n$ matrix with $1$ in the diagonal entries corresponding to where $y$ and $\hat y$ are equal, and $0$ otherwise.
Alternately, if you really want some formulas, one can use matrix algebra to say
The maximal rank diagonal matrix $A$ with only $0$ and $1$ as entries for which $Ay = A\hat y$
An alternate way is as follows:
Let $\hat y=(\hat y_1,\hat y_2,...,\hat y_n)$ and $ y=( y_1, y_2,..., y_n)$.
Define $M=diag(a_{11}, a_{22},....,a_{nn})$ as $a_{ii}=1$ if $\hat y_i=y_i$ and $a_{ii}=0$, otherwise.