Announcement

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

  • How get column name matrix for especific condition

    Hi all,

    I wonder if its possible get column name matrix for a especific condition ie: max value in the matrix.

    Until now I only van get the names all matrix:

    HTML Code:
    sysuse auto, clear
    tabulate foreign rep78, matcell(x)
    matrix list x
    
    x[2,5]
    one two three four five
    r1 2 8 27 9 2
    r2 0 0 3 9 9
    
    matrix colnames x= one two three four five
    mat lis x
    local ma : coln (x)

    My goal its to get the column name for the max value in the matrix= three =3 for rep78

    I try with below, but names were removed:

    HTML Code:
    mata : st_matrix("x", max(st_matrix("x")))
    matrix list x
    
        c1
    r1  27


    Please any comment i will grateful
    Thanks in advance
    Ro
    Last edited by Rodrigo Badilla; 22 Oct 2018, 21:37.

  • #2
    The maximum may not be unique, but if you are interested in any maximum, the following will do. The logic in the Mata code is that by sorting \(-X\), the maximum of \(X\) will be in the first row, and you can extract this row vector and look for the column position of the minimum element (which is the maximum in the original matrix).

    Code:
    sysuse auto, clear
    qui tabulate foreign rep78, matcell(x)
    matrix colnames x= one two three four five
    mat lis x
    local ma : coln (x)
    
    mata
    x= st_matrix("x")
    xs = sort(-x,1)
    v = xs[(1), .]
    maxcol = selectindex(v:==rowmin(v))
    st_numscalar("max", maxcol)
    end
    
    local maxcol= max 
    local maxcolname: word `maxcol' of `ma'
    di "`maxcolname'"
    This yields

    Code:
    . mat lis x
    
    x[2,5]
          one    two  three   four   five
    r1      2      8     27      9      2
    r2      0      0      3      9      9
    
    
    . di "`maxcolname'"
    three

    Comment


    • #3
      Thanks Andrew for you reply!!

      Your code works great!

      I did try the make the same for row names, but maybe I am doing something wrong:

      Code:
      sysuse auto, clear
      asdoc tabulate foreign rep78, chi matcell(x) replace
      matrix list x
      local me = x[1,1]
      matrix colnames x= one two three four five
      matrix rownames x= juan pedro
      mat lis x
      local ma : coln (x)
      local ma2 : rown (x)
      
      mata
      
      *for col names
      *your example
      
      x= st_matrix("x")
      xs = sort(-x,1)
      v = xs[(1), .]
      maxcol = selectindex(v:==rowmin(v))
      st_numscalar("max", maxcol)
      
      local maxcol= max
      local maxcolname: word `maxcol' of `ma'
      di "`maxcolname'"
      
      *for row names,
      *my effort ;(
      
      xs = sort(1,-x)
      v = xs[(1), .]
      maxrow = selectindex(v:==colmin(v))
      st_numscalar("max", maxrow)
      end
      
      local maxrow= max
      local maxrowname: word `maxrow' of `ma2'
      di "`maxrowname'"

      Comment


      • #4
        Rodrigo

        xs = sort(-x,1)
        This sorts only in terms of the first column. So, disregard the code in #2 because we want to sort in terms of all columns. Here is an alternative code that will pick the column max across all columns. We have the following 5\(\times\) 4 matrix

        Code:
        clear
        mat x= (12,15, 25, 2\ 2, 75, 55, 3\  112, 65, 25, 5\ 27, 27, 27, 27\ 3, 2, 1, 55)
        matrix colnames x= one two three four
        mat list x
        Code:
        . mat list x
        
        x[5,4]
              one    two  three   four
        r1     12     15     25      2
        r2      2     75     55      3
        r3    112     65     25      5
        r4     27     27     27     27
        r5      3      2      1     55
        Code:

        Code:
        clear
        mat x= (12,15, 25, 2\ 2, 75, 55, 3\  112, 65, 25, 5\ 27, 27, 27, 27\ 3, 2, 1, 55)
        matrix colnames x= one two three four
        local ma : coln (x)
        
        mata
        x= st_matrix("x")
        xs = colmax(x)\colmax(x)\colmax(x)\colmax(x)
        v = xs[(1), .]
        maxcol = selectindex(v:==rowmax(v))
        st_numscalar("max", maxcol)
        end
        
        local maxcol= max
        local maxcolname: word `maxcol' of `ma'
        di "`maxcolname'"
        Here, I use the line in red to pick out the maximum elements across each of the 4 columns. If you post in the Mata board, they can suggest a more efficient method of doing this as my Mata skills are not too developed. For rows, use the same code with the transpose of the matrix

        Code:
        clear
        mat x= (12,15, 25, 2\ 2, 75, 55, 3\  112, 65, 25, 5\ 27, 27, 27, 27\ 3, 2, 1, 55)
        matrix colnames x= one two three four
        local ma : coln (x)
        local ma2 : rown (x)
        mat xp= x'
        
        
        mata
        x= st_matrix("xp")
        xs = colmax(x)\colmax(x)\colmax(x)\colmax(x)
        v = xs[(1), .]
        maxcol = selectindex(v:==rowmax(v))
        st_numscalar("max", maxcol)
        end
        
        
        local maxrow= max
        local maxrowname: word `maxrow' of `ma2'
        di "`maxrowname'"
        Remember with multiple maximum values, the row and column names may not refer to the same element.

        Code:
        x[5,4]
              one    two  three   four
        r1     12     15     25      2
        r2      2     75     55      3
        r3    112     65     25      5
        r4     27     27     27     27
        r5      3      2      1     55
        
        
         local maxcol= max
        . local maxcolname: word `maxcol' of `ma'
        . di "`maxcolname'"
        one
        
         local maxrow= max
        . local maxrowname: word `maxrow' of `ma2'
        . di "`maxrowname'"
        r3
        Last edited by Andrew Musau; 23 Oct 2018, 09:33.

        Comment


        • #5
          Thanks Andrew! your code works great!

          Was not so easy like I thought!

          Regards

          Comment

          Working...
          X