Announcement

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

  • Calling a matrix column

    Hello everyone,
    I'm new in Mata programing, but I was convinced to start with.

    I'd like to call the columns of a matrix (i.e. in a foreach loop), and for each one check whether an element is positive or null.

    If I were in Stata, with a matrix whose columns would be name col_1 to col_n I would have done :
    Code:
    forvalues i=1/n {
    gen neighbor_`i'=1 if col_`i'>0
    ...
    }
    However this only works with variables named col_*
    In order to gain in generality (for programming purposes), I'd like it to work for any matrix.

    Nb : the goal is to create some routine to compute indexes of network analysis (such as average nearest neighbor degree, etc...) that are not included yet in existing user written program. Such statistics are not very difficult to compute (I've succeeded in Stata as said above), but I lack Mata intuition for the start.

    Best,
    Charlie

  • #2
    for example...


    Code:
    . mata:
    ------------------------------------------------- mata (type end to exit) -------------------------------------------------------------------------------------------------------------------------------------------------------------------
    : N = 5
    
    : col = -1 :+2*runiform(N,N)
    
    : neighbor = col:>0
    
    : 
    : col
                      1              2              3              4              5
        +----------------------------------------------------------------------------+
      1 |   .5535942502    .2862280291    -.233341082   -.9885533233    .7544465656  |
      2 |   .3052798635   -.5933945505    .2726562144    -.267946084     .727444212  |
      3 |   .1821316923    .2610602165   -.6498019709   -.7818916738    .2841983172  |
      4 |   .0274396609    .7491672942   -.9870268228   -.0207638359     .695635614  |
      5 |   .2214753367    .2882184442   -.0939227543   -.7124948921    .0807374776  |
        +----------------------------------------------------------------------------+
    
    : neighbor
           1   2   3   4   5
        +---------------------+
      1 |  1   1   0   0   1  |
      2 |  1   0   1   0   1  |
      3 |  1   1   0   0   1  |
      4 |  1   1   0   0   1  |
      5 |  1   1   0   0   1  |
        +---------------------+
    
    : 
    : end
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    .
    Code:
    mata:
    N = 5
    col = -1 :+2*runiform(N,N)
    neighbor = col:>0
    
    col
    neighbor
    
    end
    Use st_data/st_view to make a matrix with data from your dataset

    Comment


    • #3
      Thank you Christophe,

      It was much more simple than I thought.

      I managed to do the rest by myself (and the help of beamer by Christopher Baum )
      Thanks again,
      Charlie

      Comment

      Working...
      X