How to extract the main diagonal of a matrix? I know my question is very silly, but I cannot figure it out.
I have a $m \times m$ matrix $A$. I want to create vector $B$ such that its elements are the diagonal elements of matrix $A$. i.e. $i=j$, there for size of vector $B$ is gonna be $1 \times m$.
Can you help me how to right in math in a correct way? Thanks.
 A: Given $\mathrm A \in \mathbb R^{n \times n}$,
$$\mbox{diag}^{-1} (\mathrm A) := \sum_{i=1}^n \left(\mathrm e_i^{\top} \mathrm A \, \mathrm e_i\right) \mathrm e_i = \sum_{i=1}^n \mbox{tr} \left( \mathrm e_i \mathrm e_i^{\top} \mathrm A \right) \mathrm e_i = \sum_{i=1}^n \langle \mathrm e_i \mathrm e_i^{\top}, \mathrm A \rangle \, \mathrm e_i$$
A: I would write:
Let the $ij^{th}$ element of $A$ be $a_{ij}$ and the $ij^{th}$ element of B be $b_{ij}$, then $b_{ij} = a_{mm}$ where $B=M_{1\times m}$.
A: You have a matrix $A$ such that
$$A = \begin{pmatrix}
a_{11} & a_{22} & \ldots & a_{1n} \\
a_{21} & a_{22} & \ldots & a_{2n} \\
\vdots & \vdots & \ddots & \vdots \\
a_{n1} & a_{n2} & \ldots & a_{nn}
\end{pmatrix}
$$
Hence your vector $B$ would be
$$B = (b_1, b_2, b_3, \ldots, b_n) \equiv (a_{11}, a_{22} , a_{33}, \ldots ,  a_{nn})$$
Or also
$$B = \text{diag}\ (A)$$
The elements of $B$ will be the $a_{nn}$ elements of $A$, namely those for which $m = n$, that is the major diagonal elements.
So the $n-$th element of $B$ will be the term $a_{nn}$
For example the thord term of the vector $B$, that is $b_3$, will be $a_{33}$
