how to extract the diagonal entries out of a matrix

John W. Eaton jwe at octave.org
Sat Feb 7 00:35:25 CST 2009


On  6-Feb-2009, David Doria wrote:

| A = rand(3,3);
| B = diag(A);

Be careful with diag on arbitrary matrices.  If your matrix happens to
sometimes be a vector with more than one element, then diag does
something completely different.  For example, try

  diag (rand (2, 3))
  diag (rand (1, 3))

You can thank Cleve for this (mis)feature.  

jwe


More information about the Help-octave mailing list