Announcement

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

  • How to determine the column-number of a matrix element?

    I have a matrix like:

    1
    +----------------+
    1 | -.4755687238 |
    2 | -7.845191301 |
    3 | -7.976995867 |
    4 | -7.968100143 |
    5 | -8.008870078 |
    6 | -7.995388117 |
    7 | -7.982097707 |
    8 | -7.985624132 |
    9 | -7.972667072 |
    10 | -7.95900065 |
    11 | -7.966202475 |
    12 | -7.980775105 |
    13 | -7.967044048 |
    +----------------+

    I have determined the smallest element of this matrix with

    colmin(b[.,1])

    It is equal to -8.008870078. Now I want MATA to determine the column-number of -8.008870078, which is 5. How can I do this?

    Sorry, this might be a typical beginner question! But I can't find a solution to my problem in the manual. Thanks a lot for any help!

    John

  • #2
    Here is one way:

    Code:
    clear*
    input a double b
    1  -.4755687238 
    2  -7.845191301 
    3  -7.976995867 
    4  -7.968100143 
    5  -8.008870078 
    6  -7.995388117 
    7  -7.982097707 
    8  -7.985624132 
    9  -7.972667072 
    10  -7.95900065 
    11  -7.966202475 
    12  -7.980775105 
    13  -7.967044048 
    end 
    putmata m = b
    mata
    for (i= 1; i <= rows(m); i++){
        if( colmin(m)==m[i]) {
            ColNum = i
        }
    }
    ColNum
    end

    Comment


    • #3
      John: Also perhaps take a look at Mata's minindex function and see if that might suit your objectives: help mf_minindex.

      Comment


      • #4
        To find a value in a vector I often use:
        Code:
        :         m = (-.4755687238, -7.845191301, 7.976995867, -7.968100143, -8.008870078, 
        >                 -7.995388117, -7.982097707, -7.985624132, -7.972667072, -7.95900065, 
        >                 -7.966202475, -7.980775105, -7.967044048)'
        
        :         select(1::rows(m), m:==colmin(m))
          5
        Kind regards

        nhb

        Comment


        • #5
          Thanks a lot to you all!

          I got Scotts approach working to select the AIC lags from varsoc and transfer it to a dfuller test:

          Code:
          varsoc ln_CPI_FRA_sa_shw1
          mata
          b = st_matrix("r(stats)")
          m = b[.,7]
          
          for (i= 1; i <= rows(m); i++){
              if( colmin(m)==m[i]) {
                  ColNum = i
              }
          }
          ColNum
          st_numscalar("optilag", ColNum)
          end
          local xlags = optilag
          dfuller ln_CPI_FRA_sa_shw1 if month >= 0 & month <= 156, lags(`xlags') trend
          And I got Niels approach working for the same task:

          Code:
          varsoc ln_CPI_FRA_sa_shw1
          mata
          b = st_matrix("r(stats)")
          m = b[.,7]
          ColNum = select(1::rows(m), m:==colmin(m))
          st_numscalar("optilag", ColNum)
          end
          local xlags = optilag
          dfuller ln_CPI_FRA_sa_shw1 if month >= 0 & month <= 156, lags(`xlags') trend
          John's approach did somehow not work, but most likely I did not try hard enough, since I have already a workable solution... Thanks a lot!



          Comment


          • #6
            you don't say which version of Stata you're using, but since version 14 there is a selectindex() function which can used in this case instead of select().

            Code:
             
             ColNum = selectindex(m:==colmin(m))

            Comment

            Working...
            X