Need to extract main diagonal to vector

classic Classic list List threaded Threaded
2 messages Options
DrD
Reply | Threaded
Open this post in threaded view
|

Need to extract main diagonal to vector

DrD
Is there an easy way to extract the elements of the main diagonal to a vector? This would basically be the inverse of the diag(x) operation. Thanks,
Sam


Reply | Threaded
Open this post in threaded view
|

Re: Need to extract main diagonal to vector

nrjank
Administrator
On Thu, Jul 9, 2020 at 12:07 PM Samuel Doughty <[hidden email]> wrote:
Is there an easy way to extract the elements of the main diagonal to a vector? This would basically be the inverse of the diag(x) operation.

According to the help for diag ('help diag' or https://octave.sourceforge.io/communications/function/diag.html), it does both functions. 

>> A = rand(1,3)
A =

   0.87334   0.41651   0.69806

>> diag(A)
ans =

Diagonal Matrix

   0.87334         0         0
         0   0.41651         0
         0         0   0.69806

>> diag(ans)
ans =

   0.87334
   0.41651
   0.69806