Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • create diagonal matrix from non-diagonal matrix

    Hi all,

    It seems that Stata 13 had a simple way to create diagonal matrix from non-diagonal matrix:
    diag(Z), Z a matrix, extracts the principal diagonal of Z to create a new matrix.
    Apparently you can't do it with Stata 15. Is there a simple alternative?

    Thank you,
    Stan

  • #2
    I can't easily check on Stata 13 but functions typically don't disappear like that. Regardless, this is I guess what you mean from within Stata 15

    Code:
    matrix foo = (1,2,3\4,5,6\7,8,9)
    
    mata: st_matrix("wanted", diag(diagonal(st_matrix("foo"))))
    
    mat li wanted
    
    symmetric wanted[3,3]
        c1  c2  c3
    r1   1
    r2   0   5
    r3   0   0   9
    and there is a likely to be a simpler way yet. Elementwise multiplication with an identity matrix springs to mind.

    Comment


    • #3
      Thank you, but I'm looking for non-Mata code. Just regular Stata.
      Stata 13 documentation is available online. For example, here's the documentation for the old diag(Z):https://www.stata.com/manuals13/m-5diag.pdf

      Comment


      • #4
        The documentation you cite is for a Mata function, which is much of my point. The give-away is m-5.

        Fact is that Stata’s matrix language remains useful and documented— and very limited and frozen. 20 or so years ago Jeroen Weesie and I wrote some add-ons. If you insist on using Stata only — a case of ignoring excellent functionality there for the asking — I think you will need to write your command to do it, although those add-ons would help too.

        Comment


        • #5
          Thank you. I didn't realize the documentation was for a Mata function.

          Comment

          Working...
          X