Announcement

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

  • Saving nonzero matrix elements

    I have a matrix that looks like this:

    [0 x x x 0 x x x x]


    where the x's are nonzero elements. I'd like to retain those nonzero elements in another matrix. I looked at a couple of things that were either close http://www.statalist.org/forums/foru...-of-the-matrix or dated http://www.stata.com/statalist/archi.../msg00136.html. Can anyone provide me guidance on the best way to accomplish this task?

  • #2
    Do you mean matrix or is it sufficient to tell you about row vectors?

    Comment


    • #3
      I want to save that matrix into another matrix like so:

      [x x x x x x x ]

      Leave out the zeros.

      Comment


      • #4
        You haven't answered my question, but

        Code:
         
        . mata
        
        : x = (0,1,0,0,2,0,0,0,3)
        
        : select(x, (x :> 0))
               1   2   3
            +-------------+
          1 |  1   2   3  |
            +-------------+

        Comment


        • #5
          Well, I thought it was obvious it was a row vector, but thanks nonetheless. Using Mata introduces some other problems. I need to know how to pass the original vector into Mata and the reduced vector out. I've tried a couple of things I've seen online without success. Still looking at this point.

          Comment


          • #6
            It was clear to me, and no doubt many others, that your example was a row vector but your question was about a matrix. It's not so obvious what's the result of omitting zeros from a matrix is to be, for example, do you omit rows, columns, or both that contain 0?

            Otherwise, looking in the help for Mata -- index and guide to functions -- Stata interface functions guides you to st_matrix().

            Code:
             
            . mat x = (0,1,0,0,2,0,0,0,3)
            
            . mata : st_matrix("x", select(st_matrix("x"), st_matrix("x") :> 0))
            
            . mat li x
            
            x[1,3]
                c1  c2  c3
            r1   1   2   3

            Comment


            • #7
              Ok. Was using st_matrix. And, yes, I looked through the Mata help. I'm just not a Mata programmer, so the learning curve is steep. Thought I needed an end statement, but that was causing my program to terminate. I'll give this a try. Thanks again.

              Comment


              • #8
                Thought I could get away with working with vectors, but found out today I'm going to need to perform this operation on a covariance matrix. It will be lower triangular and have combinations of rows and columns of zeros I want to delete. For example, I could have:

                Code:
                 
                0
                0 1
                0 1 1
                0 1 1 1
                0 1 1 1 1
                0 0 0 0 0 0
                0 1 1 1 1 0 1
                0 1 1 1 1 0 1 1
                0 1 1 1 1 0 1 1 1
                0 1 1 1 1 0 1 1 1 1

                In this case I'd like to retain:

                Code:
                 
                1
                1 1
                1 1 1
                1 1 1 1
                1 1 1 1 1
                1 1 1 1 1 1
                1 1 1 1 1 1 1
                1 1 1 1 1 1 1 1

                How would I need to modify the code to perform this operation? Thanks in advance.

                Comment


                • #9
                  Someone explain this. I have matrix v with lower triangular form like above, except real number rather than ones. I can list its contents by saying:

                  Code:
                  matrix list v
                  Now, if I say:

                  Code:
                  mata : submat1=select(v, (0\1\1\1\1\0\1\1\1\1))
                  I get:

                  Code:
                  <ismt>:  3499  v not found
                  
                  r(3499)
                  and termination. So it's not finding the matrix input. Yet it's clearly there. I'd appreciate some insight as to what the problem is and how I might solve it.

                  Comment


                  • #10
                    Mata doesn't know about Stata's matrices, or indeed vice versa. That's the reason for st_matrix(), which you were using earlier.

                    Comment


                    • #11
                      I tried:

                      Code:
                      mata : st_select(st_matrix("v1"), st_matrix("v"), (0\1\1\1\1\0\1\1\1\1))
                      but that produced a conformability error. v is a 10X10 matrix, so I don't understand.

                      Comment


                      • #12
                        I'd move this over to the Mata forum and start a new thread.

                        One of the main reasons for the separate existence of the Mata forum was so that StataCorp developers could spot Mata questions easily.

                        Comment


                        • #13
                          Good idea. Thanks for your feedback.

                          Comment

                          Working...
                          X