 View Question
Q: Converting a vector to a diagonal matrix to vector using matrix operations ( No Answer,   3 Comments ) Question
 Subject: Converting a vector to a diagonal matrix to vector using matrix operations Category: Science Asked by: ipeirotis-ga List Price: \$5.00 Posted: 25 Sep 2006 21:22 PDT Expires: 25 Oct 2006 21:22 PDT Question ID: 768440
 ```I would like to know how I can convert a vector v to a diagonal matrix D, so diagonal of D, diag(D)=v. The opposite is rather easy (multiply D with the vector 1, which contains the scalar 1 in all the places). But I am a little bit confused with the opposite direction. I want to do that using only matrix operations, not by describing this operationally (e.g., for a computer program).``` Request for Question Clarification by hedgie-ga on 26 Sep 2006 03:00 PDT ```Do you allow matrices of dimension 3? (dimension is number of indexes) http://planetmath.org/encyclopedia/Tensor.html``` Clarification of Question by ipeirotis-ga on 27 Sep 2006 15:21 PDT `No, I am talking about simple n x m matrixes of 2 dimensions, and m x 1 vectors.` There is no answer at this time. ```I don't think it's possible. If it is possible, then there is a 1xN row vector where (given Nx1 column vector) x (1xN row vector) = (desired NxN diagonal matrix) This represents N unknowns (the row vector elements), and N^2 equations (forming the desired elements of the diagonal matrix, including the off-diagonal zeroes). For N > 2, there are more equations than unknowns, and the problem is not solveable.```
 ```Correction to my earlier comment: in the last line, that should be "N greater than OR EQUAL to 2"```
 ```Well, there is a solution but it is not simple or straightforward, unlike its inverse that is very simple, as you demonstrated. We have to work in steps to get what we need. This problem is nice, because it requires thinking "out of the box", so to speak. As redbelly98-ga says the simple approach gets you nowhere. Therefore first we must see if there is a simpler problem that we can solve as an intermediate step. Such a problem that works here is to start with the m element column vector V=(v1,v2,...Vm), and one of its elements Vi, and try to build a matrix Di with dimensions nxm that is zero everywhere, except in coordinates (i,i), that is the ith diagonal value, where Di(i,i)=vi. It is zero everywhere else as noted, including the rest of the diagonal. If this is possible for all i where 1<=i<=m then adding these matrices together gives you your D, as the Di contribute exactly what you need in the diagonal, and nothing else, So D=D1+...+Dm. Note also that we must have n>=m (I have some comments on that at the end). This is necessary if the diagonal is to have m elements to fit the whole of v. So lets do this in more detail: Step 1: Given: v=(v1,...,Vm) a column vector (i.e mx1 matrix) and some number i s.t. 1<=i<=m Produce Di with matrix operations Since Di uses only the number Vi, it helps if we can produce a vector for vi (call it vvi) defined as vvi=(0,...0,vi,0...0) with vi in positon i, where vvi is a ROW vector (that is an mx1 matrix) Why it is best to have a row vector will be apparent in how we use vvi. So Step 1a: Produce vvi with matrix operations Now at last this is a bit easier. Let vT be the transpose of v, that is v is turned to a row instead of the column it originally was. Transposing is a matrix operation that "mirrors" rows in columns and vice versa, turning an mxn matrix to an nxm matrix. Take also the constant matrix I_(mxm,i)= (1 in position (i,i), 0 elsewhere) This is produced from the identity matrix if we turn to zeros all diagonal elements except the ith one, or if you like from the zero matrix by turning to 1 the (i,i) position. It is constant for given m and i, as is the case here. So then vvi=VT x I_(mxm,i) To see this try to see how the multiplication works here. Note first that all columns in I_(mxm,i) are filled with zeros, except column i. So when we use the row by column rule in matrix multiplication, regardless of the values in V all results in vvi, except the result in position i, become zero. Now in position i we use column i of the matrix, which has a 1 value in position i, and so when we multiply we get V1*0+...+v(i-1)*0+vi*1+v(i+1)*0+...vm*0 = vi which shows (at last) that the formula for vvi is correct, completing step 1a. Don't worry its all downhill (relatively) from here. Now we need to do Step 1b: Find Di from vvi using matrix operations This is much easier. First note that i<=m<=n so i<=n Take the constant nx1 column vector e_(n,i) = (0,...0,1,0,...0) with the 1 occuring in position i, while there are now n, not m elements in it. You can also think of I as column i of the nxn identity matrix (obviously if m < n, then the last m-n rows are never used, but this is not a problen in any way) Again e_(n,i) is constant once we know n and i. Then finally Di= e_(n,i) x vvi See what happens here: we multiply an nx1 matrix with an 1xm matrix, so the result is an mxn matrix. The dimensions are compatible. Now for the values: In every position (j,k) we multiply row j of e_(n,i) (1 element) with column k of vvi (again 1 element). If one or the other of these elements is zero the result is zero. This happens everywhere except position (i,i) where the result is found if we multiply 1 (from e_(n,i)) with vi (from vvi), and is therefore vi. That means that the formula computes Di correctly, and we have completed step 1b. Putting both steps together to finish step 1 we get Di= e_(n,i) x VT x I_(mxm,i) producing Di from v and matrix operatons (transpose and multiplications). Now to finish the job. Step 2: Produce D from all the Di with matrix operations. As we discussed above D = D1+...+Dm = e_(n,1) x VT x I_(mxm,1) + ... + e_(n,m) x VT x I_(mxm,m) = Sum_(from i=1)^(to i=m) [ e_(n,i) x VT x I_(mxm,i) ] which gives the correct result as shown in the discussion before step 1. And we are done! And purely with matrix operations. Two comments here: 1) This solves the problem as stated (compute D with matrix operations). As an algorithm it is VERY inefficient. A straightforward copying of the diagonal values in a zeroed matrix is much easier. So don't use it in a practical implementation in a computer. I realize ipeirotis-ga knows this from the way he stated the question, but the formula might temp someone else. 2) It is common sense that n must be no smaller that m, if the diagonal is to have m elements. If instead we have n < m then e_(n,i) cannot be defined when i > n in a straightforward way (remember that it has only n elements, and it must have a value of 1 in position i, which is a contradiction because there is no position i if i > n). The best we can do is to make e_(n,i) be all zeros in this case. This works reasonably well, because if you look at the formula you see that it gives the first n elements in the diagonal, and drops the rest, which is OK, because there is no place to put the rest! So there it is. Not simple, but doable.``` 