# Extracting vector containing the elements of the main diagonal of a matrix [duplicate]

Is there any mathematical operation that would extract the elements of the main diagonal as a vector? i.e. multiply it by certain vectors or something like that. I'm using this in the context of linear systems.

In the specific case I'm looking at I have a relationship between the elements of three vectors as follows:

$\bf{a} = \begin{bmatrix} a_{1} \\ a_{2} \\ a_{3} \\ a_{4} \end{bmatrix}$ , $\bf{b} = \begin{bmatrix} b_{1} \\ b_{2} \\ b_{3} \\ b_{4} \end{bmatrix}$ , and $\bf{c} = \begin{bmatrix} c_{1} \\ c_{2} \\ c_{3} \\ c_{4} \end{bmatrix}$

I also know that: $c_{i} = a_{i}b_{i}$ for $i \in [1, 4]$

Now I want to express this relationship as a vector equation. I understand that $\bf{a} \bf{b}^\top$ would give a square matrix with the elements of $\bf{c}$ on its main diagonal, but is there anyway to extract them as a vector?

EDIT: Let me clarify a bit. If I multiply $\bf{a}$ by $\bf{b}^\top$ I get the following matrix:

$\bf{a} \bf{b}^\top = \begin{bmatrix} \bf{a_1b_1} && a_1b_2 && a_1b_3 && a_1b_4 \\ a_2b_1 && \bf{a_2b_2} && a_2b_3 && a_2b_4 \\ a_3b_1 && a_3b_2 && \bf{a_3b_3} && a_3b_4 \\ a_4b_1 && a_4b_2 && a_4b_3 && \bf{a_4b_4} \end{bmatrix}$

The elements which have been made bold are the ones I'm interested in extracting as a vector. This vector would be $\bf{c}$.

If I multiply this by the all-ones vector, as some of the answers have suggested, I would get:

$\bf{a} \bf{b}^\top \bf{1}= \begin{bmatrix} \bf{a_1b_1} && a_1b_2 && a_1b_3 && a_1b_4 \\ a_2b_1 && \bf{a_2b_2} && a_2b_3 && a_2b_4 \\ a_3b_1 && a_3b_2 && \bf{a_3b_3} && a_3b_4 \\ a_4b_1 && a_4b_2 && a_4b_3 && \bf{a_4b_4} \end{bmatrix} \begin{bmatrix} 1 \\ 1 \\ 1 \\ 1 \end{bmatrix} = \begin{bmatrix} a_1b_1 + a_1b_2 + a_1b_3 + a_1b_4 \\ a_2b_1 + a_2b_2 + a_2b_3 + a_2b_4 \\ a_3b_1 + a_3b_2 + a_3b_3 + a_3b_4 \\ a_4b_1 + a_4b_2 + a_4b_3 + a_4b_4 \end{bmatrix}$

Which is not the vector I'm looking for (it isn't equal to $\bf{c}$).

EDIT 2: Multiplying by the $\bf{1}$ vector would obviously work if all off diagonal elements become zero. So if anyone knows of a way to do that without modifying the elements of the main diagonal that would also answer my question.

EDIT 3: The other question pointed out in the comments area is essentially the same and I have received similar answers but I was hoping for a simpler solution. I haven't marked it as duplicate to allow people to contribute in the future.

I was hoping for a solution that would be linear in $\bf{b}$ which I would substitute in place of $\bf{c}$ into the equation I'm trying to solve. In that case $\bf{b}$ would be my only unknown and I would be able to get an algebraic solution.

• $c_{ii} = a_{ii} b_{ii}$ or $c_{i1} = a_{i1} b_{i1}$? Sep 16, 2016 at 10:10
• @RodrigodeAzevedo you are correct. I just edited it. Sep 16, 2016 at 12:21
• There's no need to extract any diagonal. Just use the Hadamard / Schur entrywise product $$\mathrm c = \mathrm a \circ \mathrm b$$ Sep 16, 2016 at 15:52
• @Hadi, it should be feasible, if you accept the operation of stacking/joining vectors into a matrix Sep 16, 2016 at 23:11
• @RodrigodeAzevedo I was just about to suggest a Hadamard product myself.
– amd
Sep 17, 2016 at 2:08

Your profile suggests that you ask this question from a computer science perspective. If that's the case then maybe it's not the right question.

It's easy to compute the vector you want directly by multiplying corresponding coordinates, just the way you compute the sum of two vectors by adding coordinates. Some programming languages even implement this operation as a primitive. I don't think there's a nice way to express it using common matrix operations - but do you really need that?

Mathematically, your operation is multiplication in the direct product of $n=4$ copies of the real numbers - see https://en.wikipedia.org/wiki/Product_of_rings .

• I have a STEM background and I do plan to program it so I have considered just using operations such as the "diag" function in MATLAB. However, these vectors all fit into a matrix equation that I'm trying to solve and if I'm able to form the vector using matrix operations it would allow me to solve the equation algebraically (which my PhD committee would prefer). Sep 16, 2016 at 22:18

Well, it’s not pretty, but this will do it:

$$\sum_{i=1}^4\mathbf a^T\mathbf e_i\mathbf b^T\mathbf e_i\mathbf e_i$$ where the $\mathbf e_i$ are the standard basis vectors. Each term of the sum extracts the $i$th components of $\mathbf a$ and $\mathbf v$ and multiplies them together. You can also think of it as multiplying the projection of $\mathbf b$ onto $\mathbf e_i$ by $a_i$ or vice-versa.

• This doesn't exactly solve my problem but I'm accepting it as it does specifically answer the question. Sep 16, 2016 at 22:46
• @Hadi Wish I could offer something else, but I don’t know of any non-clunky way to do this with conventional matrix operations.
– amd
Sep 17, 2016 at 2:06

You can extract the elements of the main diagonal of the matrix as a vector by multiplying your diagonal matrix $D$ by the vector $v$ having $1$ on each of its entries. If $D=diag(d_{1},...,d_{n})$ and $v=(1,...,1)^{t}$, then $$Dv = (d_{1},...,d_{n})^{t}.$$

• Please look at my edits. This wasn't what I was looking for. Thanks. Sep 16, 2016 at 12:40

[I’m adding a second answer so that this option isn’t only buried in the comments. Rodrigo de Azevedo suggested it first.]

One possibility is to use the Hadamard (componentwise) product $a\circ b$. This is the simplest option by far, and meets the requirement that it be linear in $b$ to boot. Without seeing more of your equation, though, it’s impossible to say whether or not it will play nicely with the rest of your calculations.