I have a square matrix A. Is there a way I can apply operations like addition, subtraction, matrix multiplication, matrix inverse and transpose to get the diagonal of the matrix. For example having: $$\begin{pmatrix}1&2\\3&4\end{pmatrix}$$ I would like to get $(1,4)$.

P.S. based on the conversation with mvw, here is a better description:

I am on board of an alien space ship and the board computer allows only matrix operations but access to the individual matrix elements is blocked. I can only use addition, subtraction, matrix multiplication, matrix inverse and transpose. No access to individual row/column/element. I can only create matrices of any dimension $(1 x n)$, $(n x 1)$, $(n x 2n)$ that have all zeros or all ones. Is there a way for me to get a diagonal vector?

  • $\begingroup$ What do you mean by extracting? Something like $(a_{11},a_{22},..,a_{nn})$ denotes the diagonal entries... $\endgroup$ Nov 13, 2015 at 20:33
  • $\begingroup$ @YoTengoUnLCD yes. I want to get a vector of diagonal elements from the square matrix. $\endgroup$ Nov 13, 2015 at 20:46
  • $\begingroup$ @mvw something similar :-). I can only use addition, subtraction, matrix multiplication, matrix inverse and transpose. No access to individual row/column/element. I can't even create other matrices. I am wondering whether I can somehow get the diagonal vector from my matrix. $\endgroup$ Nov 13, 2015 at 20:51
  • $\begingroup$ So only the matrix $A$ is in the game? The best you can get is a diagonal matrix, because all of the above operations will create $n\times n$ matrices. $\endgroup$
    – mvw
    Nov 13, 2015 at 20:57
  • $\begingroup$ @mvm thanks. the best you can get is diagonal matrix is an excellent observation. P.S. I can also create any matrix with all zeros and with all ones. $\endgroup$ Nov 13, 2015 at 21:11

3 Answers 3


Note: This solution is not working for the updated question. $$ D = \text{diag}(a_{11}, \ldots, a_{nn}) = \sum_{i=1}^n P_{(i)} A P_{(i)} $$ where $P_{(i)}$ is the projection on the $i$-th coordinate: $$ (P_{(i)})_{jk} = \delta_{ij} \delta_{jk} \quad (i,j,k \in \{1,\ldots,n\}) $$ and $\delta$ is the Kronecker delta ($1$ for same index values, otherwise $0$).

Transforming the diagonal matrix $D$ into a row vector can be done by $$ d = u^T D $$ where each of the $n$ components of $u$ is $1$. $$ u = (1,1,\ldots,1)^T $$ Combining both gives $$ d = \sum_i u^T P_{(i)} A P_{(i)} = \sum_i e_i^T A P_{(i)} $$ where $e_i$ is the $i$-th canonical base vector.


octave> A, P1, P2, u
A =
   1   2
   3   4

P1 =
   1   0
   0   0

P2 =
   0   0
   0   1

u =

octave> u'*(P1*A*P1+P2*A*P2)
ans =
   1   4

If $A$ is a $(2\times 2)$ matrix then $$ \def\LR#1{\left(#1\right)} \operatorname{Diag}(A) = \LR{e_1^TAe_1}e_1 + \LR{e_2^TAe_2}e_2 \\ $$ where $\{e_k\}$ denote the standard basis vectors.

This can be extended to higher dimensions $$ \operatorname{Diag}(A) = \sum_{k=1}^n \LR{e_k^TAe_k}e_k \\ $$


Absolutely! Although mvw's answer is correct, it might be a little more than you are looking for. Such a technique is actually a really big theorem in Linear Algebra. All we do is apply elementary row operations to you matrix: $$\begin{bmatrix} 1 & 2 \\ 3 & 4 \\ \end{bmatrix} \rightarrow_{(1/3)R_2} \begin{bmatrix} 1 & 2 \\ 1 & 4/3 \\ \end{bmatrix} \rightarrow_{R_2 - R_1} \begin{bmatrix} 1 & 2 \\ 0 & (4/3)-(6/3) \\ \end{bmatrix} \rightarrow \begin{bmatrix} 1 & 2 \\ 0 & (-2/3)\\ \end{bmatrix} \rightarrow_{(-6)R_2} \begin{bmatrix} 1 & 2 \\ 0 & 4 \\ \end{bmatrix}$$ From here, it should seem like a sinch to comlplete the steps. We note that each of the row operations is expressible as an elementary matrix of type, 1, 2, or 3.

  • 1
    $\begingroup$ Note, this isn't a complete solution, you still have to get rid of the $2$ in the $(1,2)$ entry. $\endgroup$
    – 9301293
    Nov 13, 2015 at 20:41
  • $\begingroup$ I need to get a vector from the matrix, here you create a matrix with all elements are zero except of diagonal. $\endgroup$ Nov 13, 2015 at 20:46
  • $\begingroup$ @SalvadorDali note that if you multiply your diagonal matrix by $[1,1]^T$ then you have your answer. $\endgroup$
    – 9301293
    Nov 13, 2015 at 20:55
  • 2
    $\begingroup$ I”m skeptical. Please show me a sequence of elementary row operations that transforms $\pmatrix{0&1\\1&0}$ into $\pmatrix{0&0\\0&0}$. Also, if you ever develop a zero along the main diagonal during this process, as you will for a singular matrix, there’s a good chance that you won’t be able to recover the original element in that position. $\endgroup$
    – amd
    Sep 17, 2016 at 2:11
  • 3
    $\begingroup$ @phatty I'm a bit confused as to how you "knew" to multiply by -6 in the last step. It seems to me that while all of the other operations are consistent for reducing the matrix to solve along its dimensions, that last step requires some knowledge of the desired answer, and picking the number required to produce it. $\endgroup$
    – Edwin Buck
    May 16, 2018 at 2:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .