Announcement

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

  • removing zeros from a vector

    Hi Statalist,

    Do you know a simple way to remove the zero elements from a vector while preserving the row/column names of the maintained elements? As far as I know, these row/column names are removed by mata select() function.

    Many thanks for your attention!

  • #2
    Say rather that select() never sees the row names or column names in what it sees within Mata. The row and column names disappear on import and never get reinstated.



    Comment


    • #3
      Code:
      clear all
       input x
       1
       2
       3
       4
       5
       0
       0
       end
      
      mkmat x , matrix(X)
      mata: X = st_matrix("X")
      mata: M=select(X, X[.,1]:>0)
      mata: Z=st_matrix("Z",M)
      mat list X
      mat list Z
      HTML Code:
      . mat list X
      
      X[7,1]
          x
      r1  1
      r2  2
      r3  3
      r4  4
      r5  5
      r6  0
      r7  0
      
      . mat list Z
      
      Z[5,1]
          c1
      r1   1
      r2   2
      r3   3
      r4   4
      r5   5
      Emad A. Shehata
      Professor (PhD Economics)
      Agricultural Research Center - Agricultural Economics Research Institute - Egypt
      Email: [email protected]
      IDEAS: http://ideas.repec.org/f/psh494.html
      EconPapers: http://econpapers.repec.org/RAS/psh494.htm
      Google Scholar: http://scholar.google.com/citations?...r=cOXvc94AAAAJ

      Comment


      • #4
        Emad: Your code illustrates the problem, not a solution. Z has the default row and column names. The original column name has disappeared.

        Also, stripping 0s at the end is the easiest case.

        I don't have a solution at the moment either. It's easy enough to save the row and column names safely, but the trick is knowing which to put back.

        This is programmable. but I am hoping that someone has done it already. matselrc from STB-56 isn't general enough by far, and long predates Mata.



        Comment


        • #5
          Thanks dear Nick for illustration
          Emad A. Shehata
          Professor (PhD Economics)
          Agricultural Research Center - Agricultural Economics Research Institute - Egypt
          Email: [email protected]
          IDEAS: http://ideas.repec.org/f/psh494.html
          EconPapers: http://econpapers.repec.org/RAS/psh494.htm
          Google Scholar: http://scholar.google.com/citations?...r=cOXvc94AAAAJ

          Comment


          • #6
            Well, st_matrixcolstripe() and st_matrixrowstripe() obtain and set column/rownames. It should be simple to use the same vector to select the names associated with the non-zero columns/rows.

            Best
            Daniel

            Comment


            • #7
              Thanks Dear Daniel
              I will try your command
              Emad A. Shehata
              Professor (PhD Economics)
              Agricultural Research Center - Agricultural Economics Research Institute - Egypt
              Email: [email protected]
              IDEAS: http://ideas.repec.org/f/psh494.html
              EconPapers: http://econpapers.repec.org/RAS/psh494.htm
              Google Scholar: http://scholar.google.com/citations?...r=cOXvc94AAAAJ

              Comment


              • #8
                Here is a sketch. The error checking is poor but it illustrates the basic idea

                Code:
                version 12.1
                
                mata :
                
                void delete_0(string scalar name, string scalar newname)
                {
                    real matrix X
                    string matrix cnames, rnames
                    real colvector s
                    
                    X = st_matrix(name)
                    cnames = st_matrixcolstripe(name)
                    rnames = st_matrixrowstripe(name)
                    
                    s = (X :!= 0)
                    
                    if (orgtype(X) == "colvector") {
                        rnames = select(rnames, s)
                    }
                    else if (orgtype(X) == "rowvector") {
                        cnames = select(cnames, s)
                    }
                    else {
                        _error(3201)
                    }
                    
                    st_matrix(newname, select(X, s))
                    st_matrixcolstripe(newname, cnames)
                    st_matrixrowstripe(newname, rnames)
                }
                
                end
                
                clear
                input x
                1
                2
                3
                4
                5
                0
                0
                end
                
                mkmat x , matrix(X)
                matrix rownames X = a b c d e f g
                mata : delete_0("X", "Z")
                matrix list X
                matrix list Z
                Best
                Daniel

                Comment


                • #9
                  Thanks dear Nick and Daniel
                  I think it is a good idea to make ado user written code about this matrix name
                  Best regards
                  Emad A. Shehata
                  Professor (PhD Economics)
                  Agricultural Research Center - Agricultural Economics Research Institute - Egypt
                  Email: [email protected]
                  IDEAS: http://ideas.repec.org/f/psh494.html
                  EconPapers: http://econpapers.repec.org/RAS/psh494.htm
                  Google Scholar: http://scholar.google.com/citations?...r=cOXvc94AAAAJ

                  Comment


                  • #10
                    Code:
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input float(y x1 x2) int(x3 x4)
                     99.2  96.7   101  12  28
                       99  98.1 100.1  15  35
                      100   100   100  17  37
                    111.6 104.9  90.6  22  42
                    122.2 104.9  86.5  36  47
                    117.6 109.5  89.7  45  51
                    121.1 110.8  90.6  66  56
                      136 112.3  82.8  89  60
                    154.2 109.3  70.1  99  65
                    153.6 105.3  65.4 118  69
                    158.5 101.7  61.3 134  74
                    140.6  95.4  62.5 151  78
                    136.2  96.4  63.6 167  83
                      168  97.6  52.6 184  87
                    154.3 102.4  59.7 200  92
                      149 101.6  59.5 217  96
                    165.5 103.8  61.3 233 101
                    end
                    
                    mata :
                    void delete_0(string scalar name, string scalar newname)
                    {
                        real matrix X
                        string matrix cnames, rnames
                        real colvector s
                        X = st_matrix(name)
                        cnames = st_matrixcolstripe(name)
                        rnames = st_matrixrowstripe(name)
                        s = (X :!= 0)
                        if (orgtype(X) == "colvector") {
                            rnames = select(rnames, s)
                        }
                        else if (orgtype(X) == "rowvector") {
                            cnames = select(cnames, s)
                        }
                        else {
                            _error(3201)
                        }
                     
                        st_matrix(newname, select(X, s))
                        st_matrixcolstripe(newname, cnames)
                        st_matrixrowstripe(newname, rnames)
                     }
                     end
                    
                     constr def 1 x2=0
                     cnsreg y x1 x2 x3 x4 , c(1)
                     mat X= e(b)'
                     local nX : rowfullnames X
                     matrix rownames X = `nX'
                     mata : delete_0("X", "Z")
                     matrix list X
                     matrix list Z
                    HTML Code:
                    X[5,1]
                                  y1
                       x1  .68139522
                       x2          0
                       x3  .00413235
                       x4  .93034973
                    _cons  3.6416136
                    
                    .  matrix list Z
                    
                    Z[4,1]
                                  y1
                       x1  .68139522
                       x3  .00413235
                       x4  .93034973
                    _cons  3.6416136
                    Emad A. Shehata
                    Professor (PhD Economics)
                    Agricultural Research Center - Agricultural Economics Research Institute - Egypt
                    Email: [email protected]
                    IDEAS: http://ideas.repec.org/f/psh494.html
                    EconPapers: http://econpapers.repec.org/RAS/psh494.htm
                    Google Scholar: http://scholar.google.com/citations?...r=cOXvc94AAAAJ

                    Comment

                    Working...
                    X