Announcement

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

  • Saving nonzero matrix elements

    I have a covariance matrix that is lower triangular where the ones represent values:

    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
    I'd like to delete the rows and columns that are all zero:

    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
    I know that there is probably some way of doing this with st_select and st_matrix in mata, but everything I've tried has been met with a error. Can someone help me out?

  • #2
    Could you create a reproducible example with code? I'm thinking like

    Code:
    1, 1 \ 0, 1
    both for input and desired output.

    I am not really clear on what a covariance matrix object *is* in Mata, since I've never come across one.

    Comment


    • #3
      Here is one solution. You can make a function out of this.
      In this example J should be triangular where the second row and the third column have zeros and should be removed.
      Code:
      J =(1,0,0,0,0\0,0,0,0,0\1,1,0,0,0\1,1,0,1,0\1,1,0,1,1)
      
      
      I=select(J,rowsum(J):!=0)
      I=select(I,colsum(I):!=0)

      Comment


      • #4
        As I read
        where the ones represent values
        all sorts of numbers can be placed instead of ones.
        If so Christophe's solution can not be used directly, take for instance
        Code:
        J =(1,0,0,0,0\0,0,0,0,0\1,-1,0,0,0\1,1,0,1,0\1,1,0,1,1)
        Kind regards

        nhb

        Comment


        • #5
          Niels, you're right. Thanks for pointing that. We can then modify the code as follows
          Code:
          I = J:!=0
          I = select(I,rowsum(I):!=0)
          I = select(I,colsum(I):!=0)

          Comment


          • #6
            Ah, excellent. I tried to figure out that solution myself, but I got stuck at the one thing missing in your solution as well.
            We have to select the rows and columns of J, not I, so I think the answer is:
            Code:
            J = select(J,rowsum(J:!=0):!=0)
            J = select(J,colsum(J:!=0):!=0)
            Or something like it
            Last edited by Niels Henrik Bruun; 18 Jun 2015, 06:56.
            Kind regards

            nhb

            Comment


            • #7
              Seems right to me .
              What about this one (if you would like to keep J)?
              Code:
              rIndex = select((1..rows(J))',rowsum(J:!=0):!=0)
              cIndex = select((1..cols(J)),colsum(J:!=0):!=0)
              
              I = J[rIndex,cIndex]

              Comment


              • #8
                That double :!= condition looks a little complicated. I would define

                Code:
                test    = !!J
                rIndex  = select(1::rows(J),rowsum(test))
                cIndex  = select(1..cols(J),colsum(test))
                select already treats nonzero/zero as true/false.

                Comment


                • #9
                  !!J is very elegant. Since the original question proposed a covariance (and thus symmetric), matrix, taking this into account reduces the number of selects, since each row of zeros corresponds to a column of zeros.
                  Code:
                  mata:
                  J =(1,0,0,1,1\0,0,0,0,0\0,0,2,-2,0\1,0,-2,1,0\1,0,0,0,1)
                  index  = select(1::rows(J),rowsum(!!J))
                  I = J[index,index]
                  J
                  I
                  end
                  Last edited by William Lisowski; 18 Jun 2015, 14:06.

                  Comment

                  Working...
                  X