Announcement

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

  • Dropping repeated columns from a matrix

    Dear statalist,

    I have a matrix in which two or more columns are exactly same. Is there some way of keeping one of them and dropping all others?

    For example, I have the matrix

    1 4 2 1 1
    2 7 0 2 2
    3 3 5 3 3

    I want to drop the last two columns since they are same as the 1st column.

    Thank you in advance.

    Shoummo Sen Gupta

  • #2
    Mata seems to have a command or function for almost any imaginable operation on matrices, but I don't know one for this. That's probably just one of many gaps in my knowledge of Mata, and perhaps somebody else will show you a simpler solution. But this will do it:

    Code:
    clear*
    
    matrix M = (    1, 4, 2, 1, 1 \ ///
                    2, 7, 0, 2, 2 \ ///
                    3, 3, 5, 3, 3)
                   
    matrix list M
    
    matrix MT = M'
    
    svmat MT
    duplicates drop
    
    mkmat MT*, matrix(MT)
    matrix M = MT'
    
    matrix list M

    Comment


    • #3
      Mata has a uniqrows() function for that. Obviously, you need to transpose the matrix before using the function. A side-effect is that the matrix is sorted. My elabel (SSC) package has a Mata function, distinctrowsof(), that preserves the original sort order.

      Best
      Daniel

      Comment


      • #4
        Thanks all.. These will help.

        Comment

        Working...
        X