Announcement

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

  • Dropping Columns in Matricies

    Hi hello everyone!

    I am coming to you with a question about matrices. I have an NxN matrix. I want to delete the row "i" so that the matrix would have (N-1)x(N-1) dimensions. Does anyone know how to do that?

    Thanks for help!
    Daniel

  • #2
    If you delete the i'th row, the matrix will have (N-1)xN dimensions. Do you mean you want to delete both the i'th row and the i'th column?

    Comment


    • #3
      One way is through submatrix extraction, excluding the to be deleted rows/columns. As Clyde states, deleting a column in an \(N \times N\) matrix results in a \(N \times N-1\) matrix.

      Code:
      help matrix_extraction
      Code:
      sysuse auto, clear
      keep in 1/7
      mkmat price mpg rep78 headroom trunk weight length, mat(auto)
      mat l auto
      *SUPPOSE THAT i=3
      mat auto= auto[1...,1..2], auto[1..., 4...]
      mat l auto
      Res.:

      Code:
      . mat l auto
      
      auto[7,7]
             price       mpg     rep78  headroom     trunk    weight    length
      r1      4099        22         3       2.5        11      2930       186
      r2      4749        17         3         3        11      3350       173
      r3      3799        22         .         3        12      2640       168
      r4      4816        20         3       4.5        16      3250       196
      r5      7827        15         4         4        20      4080       222
      r6      5788        18         3         4        21      3670       218
      r7      4453        26         .         3        10      2230       170
      
      . *SUPPOSE THAT i=3
      
      . mat auto= auto[1...,1..2], auto[1..., 4...]
      
      . mat l auto
      
      auto[7,6]
             price       mpg  headroom     trunk    weight    length
      r1      4099        22       2.5        11      2930       186
      r2      4749        17         3        11      3350       173
      r3      3799        22         3        12      2640       168
      r4      4816        20       4.5        16      3250       196
      r5      7827        15         4        20      4080       222
      r6      5788        18         4        21      3670       218
      r7      4453        26         3        10      2230       170
      
      .
      ADDED IN EDIT: #1 asks about deleting a row, in which case you have:

      Code:
      sysuse auto, clear
      keep in 1/7
      mkmat price mpg rep78 headroom trunk weight length, mat(auto)
      mat l auto
      *SUPPOSE THAT i=3
      mat auto= auto[1..2, 1...]\auto[4..., 1...]
      mat l auto
      Res.:

      Code:
      . mat l auto
      
      auto[7,7]
             price       mpg     rep78  headroom     trunk    weight    length
      r1      4099        22         3       2.5        11      2930       186
      r2      4749        17         3         3        11      3350       173
      r3      3799        22         .         3        12      2640       168
      r4      4816        20         3       4.5        16      3250       196
      r5      7827        15         4         4        20      4080       222
      r6      5788        18         3         4        21      3670       218
      r7      4453        26         .         3        10      2230       170
      
      . *SUPPOSE THAT i=3
      
      . mat auto= auto[1..2, 1...]\auto[4..., 1...]
      
      . mat l auto
      
      auto[6,7]
             price       mpg     rep78  headroom     trunk    weight    length
      r1      4099        22         3       2.5        11      2930       186
      r2      4749        17         3         3        11      3350       173
      r4      4816        20         3       4.5        16      3250       196
      r5      7827        15         4         4        20      4080       222
      r6      5788        18         3         4        21      3670       218
      r7      4453        26         .         3        10      2230       170
      
      .

      Last edited by Andrew Musau; 28 Aug 2022, 11:14.

      Comment


      • #4
        Thanks a lot! It worked!

        Comment

        Working...
        X