Announcement

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

  • Delete a matrix trace

    Hello,

    I would like to know how to delete the trace of a matrix. I do not find it in the "help".
    Thank you

  • #2
    What do you mean with delete?

    Comment


    • #3
      I dont know the exact term

      I have :
      A B C
      a 4 5 3
      b 5 12 7
      c 25 48 9
      I want
      A B C
      a 5 3
      b 5 7
      c 25 48

      Comment


      • #4
        Marc,
        Do you want the diagonal to be missing values (.) or zeros? You can't have blanks (strings "") in a mata matrix altogether with reals.

        If you want them to be zeros, it is easy enough :
        Code:
        M= (4,5,3 \ 5,12,7 \25,48,9)
        N=M-diag(M)
        Charlie
        Last edited by Charlie Joyez; 14 Nov 2016, 10:53.

        Comment


        • #5
          I shall prefer missing values.
          I think I have a problem of translation between French and English. A trace in French is a diagonal matrix in English.
          Last edited by Marc Audrna; 14 Nov 2016, 11:20.

          Comment


          • #6
            Code:
            mata:
            X = runiform(5,5)
            X
            _diag(X,.)
            X
            end

            Comment


            • #7
              Originally posted by Marc Audrna View Post
              I shall prefer missing values.
              I think I have a problem of translation between French and English. A trace in French is a diagonal matrix in English.
              Well not exactly, even in French, the trace of a matrix (the equivalent term also exists in English) is the sum of the diagonal elements, so is only a scalar, when the diagonal is actually a vector containing the diagonal elements.

              Concerning your initial question, Rafal gave you the code you need.

              Best,
              Charlie

              Comment


              • #8
                Thank you for you answers.
                But, none of the two method runs.

                when I type :
                Code:
                mat N=X-diag(X)
                Stata returns "conformability error". Yet, I run a square matrix X(37,37).

                And when I type
                Code:
                _diag(X,.)
                Stata returns "unrecognized command: _diag".

                I think that I miss a stage...?

                Precisions :
                I import an Excel sheet and I make a matrix whith mkmat function
                Last edited by Marc Audrna; 15 Nov 2016, 05:10.

                Comment


                • #9
                  Marc,
                  Do you write on Stata or on Mata?
                  as Rafal precised, you have to type both codes under these two delimiters:
                  Code:
                  mata:
                  
                  end
                  And your Matrices should be mata matrices and not Stata ones (we are on the Mata forum).


                  Actually, I find the same error message as you when I run under Stata, so I think this is the problem.
                  Code:
                  *Under Stata
                  matrix define X=(2,2 \ 3,3)
                  matrix list
                  mat N=X-diag(X)
                  *returns conformability error
                  
                  _diag(X,.)
                  *returns unrecognized command:  _diag
                  To define you matrix on mata:
                  Code:
                  mata :
                  X=(2,2 \ 3,3)
                  X
                  mat N=X-diag(X)
                  *does not returns error code
                  N
                  
                  _diag(X,.)
                  X
                  end
                  Best,
                  Charlie

                  Comment


                  • #10
                    Okay !
                    I do not use stata every day and I have some gaps...
                    Is it possible to make what I whant with stata or must I make it with mata ?
                    I have difficulty understanding how to pass from stata to mata...
                    Can I use mata with Do-file Editor ?

                    Comment


                    • #11
                      use st_matrix to pass matrices to mata back and forth.
                      for example

                      Code:
                      mat sX = matuniform(5,5)
                      mat li sX
                      
                      mata:
                      mX = st_matrix("sX")
                      _diag(mX,.)
                      st_matrix("sX",mX)
                      
                      end
                      
                      mat li sX

                      Comment


                      • #12
                        Thank you very much to all of you !

                        Comment

                        Working...
                        X