Announcement

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

  • Select matrix column elements based on vector elements.

    Hi everyone,

    I've searched the resources on Mata for a while and haven't found a solution yet. Here's the issue I'm having: I've created a matrix of 0's (in the example below, it's a matrix with 20 rows and 6 columns) and want to replace the 0's with 1's based on elements of another matrix (called combn_mat in the code below). Eventually, I will write a loop to go through each row of the selecting matrix (combn_mat) and select those column from the first matrix (called pmt in the example) with that index and replace them with 1's.

    So, for example:

    Code:
    . mata:
    ------------------------------------------------- mata (type end to exit) -----------------------------------------------------------------------------------------------------------------------------------------------------
    :         x=comb(6,3)
    
    :         pmt=J(x,6,0)
    
    :         pmt
            1   2   3   4   5   6
         +-------------------------+
       1 |  0   0   0   0   0   0  |
       2 |  0   0   0   0   0   0  |
       3 |  0   0   0   0   0   0  |
       4 |  0   0   0   0   0   0  |
       5 |  0   0   0   0   0   0  |
       6 |  0   0   0   0   0   0  |
       7 |  0   0   0   0   0   0  |
       8 |  0   0   0   0   0   0  |
       9 |  0   0   0   0   0   0  |
      10 |  0   0   0   0   0   0  |
      11 |  0   0   0   0   0   0  |
      12 |  0   0   0   0   0   0  |
      13 |  0   0   0   0   0   0  |
      14 |  0   0   0   0   0   0  |
      15 |  0   0   0   0   0   0  |
      16 |  0   0   0   0   0   0  |
      17 |  0   0   0   0   0   0  |
      18 |  0   0   0   0   0   0  |
      19 |  0   0   0   0   0   0  |
      20 |  0   0   0   0   0   0  |
         +-------------------------+
    
    :         combn_mat=mm_subsets(6,3)'
    
    :        
    :         combn_mat
            1   2   3
         +-------------+
       1 |  1   2   3  |
       2 |  1   2   4  |
       3 |  1   2   5  |
       4 |  1   2   6  |
       5 |  1   3   4  |
       6 |  1   3   5  |
       7 |  1   3   6  |
       8 |  1   4   5  |
       9 |  1   4   6  |
      10 |  1   5   6  |
      11 |  2   3   4  |
      12 |  2   3   5  |
      13 |  2   3   6  |
      14 |  2   4   5  |
      15 |  2   4   6  |
      16 |  2   5   6  |
      17 |  3   4   5  |
      18 |  3   4   6  |
      19 |  3   5   6  |
      20 |  4   5   6  |
         +-------------+
    
    :        
    :         y=combn_mat[7,]
    
    :         y
           1   2   3
        +-------------+
      1 |  1   3   6  |
        +-------------+
    
    :        
    :         pmt[7,y]=1      
                     <istmt>:  3301  subscript invalid
    (1 line skipped)
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    r(3301);
    Thus, in this simple example, I'd like to replace columns 1, 3, 6 in row 7 in matrix pmt with 1's. However, I'm stuck at the point of getting the (1,3,6) into a form that the subset brackets will recognize (i.e., 1,3,6). Thanks in advance for your help! I am using Stata version 14.1.

    John
    Last edited by John Gallis; 13 Feb 2017, 12:43.

  • #2
    Solved. It had to do with the way I was trying to assign the value of 1, not with the subscripting. So:

    Code:
    . mata:
    ------------------------------------------------- mata (type end to exit) -----------------------------------------------------------------------------------------------------------------------------------------------------
    :         x=comb(6,3)
    
    :         pmt=J(x,6,0)
    
    :         pmt
            1   2   3   4   5   6
         +-------------------------+
       1 |  0   0   0   0   0   0  |
       2 |  0   0   0   0   0   0  |
       3 |  0   0   0   0   0   0  |
       4 |  0   0   0   0   0   0  |
       5 |  0   0   0   0   0   0  |
       6 |  0   0   0   0   0   0  |
       7 |  0   0   0   0   0   0  |
       8 |  0   0   0   0   0   0  |
       9 |  0   0   0   0   0   0  |
      10 |  0   0   0   0   0   0  |
      11 |  0   0   0   0   0   0  |
      12 |  0   0   0   0   0   0  |
      13 |  0   0   0   0   0   0  |
      14 |  0   0   0   0   0   0  |
      15 |  0   0   0   0   0   0  |
      16 |  0   0   0   0   0   0  |
      17 |  0   0   0   0   0   0  |
      18 |  0   0   0   0   0   0  |
      19 |  0   0   0   0   0   0  |
      20 |  0   0   0   0   0   0  |
         +-------------------------+
    
    :         combn_mat=mm_subsets(6,3)'
    
    :         
    :         combn_mat
            1   2   3
         +-------------+
       1 |  1   2   3  |
       2 |  1   2   4  |
       3 |  1   2   5  |
       4 |  1   2   6  |
       5 |  1   3   4  |
       6 |  1   3   5  |
       7 |  1   3   6  |
       8 |  1   4   5  |
       9 |  1   4   6  |
      10 |  1   5   6  |
      11 |  2   3   4  |
      12 |  2   3   5  |
      13 |  2   3   6  |
      14 |  2   4   5  |
      15 |  2   4   6  |
      16 |  2   5   6  |
      17 |  3   4   5  |
      18 |  3   4   6  |
      19 |  3   5   6  |
      20 |  4   5   6  |
         +-------------+
    
    :         
    :         y=combn_mat[7,]
    
    : 
    :         
    :         for (k=1; k<=cols(y); k++) {
    >                 pmt[7,y[,k]]=1
    >         }
    
    :         
    :         pmt
            1   2   3   4   5   6
         +-------------------------+
       1 |  0   0   0   0   0   0  |
       2 |  0   0   0   0   0   0  |
       3 |  0   0   0   0   0   0  |
       4 |  0   0   0   0   0   0  |
       5 |  0   0   0   0   0   0  |
       6 |  0   0   0   0   0   0  |
       7 |  1   0   1   0   0   1  |
       8 |  0   0   0   0   0   0  |
       9 |  0   0   0   0   0   0  |
      10 |  0   0   0   0   0   0  |
      11 |  0   0   0   0   0   0  |
      12 |  0   0   0   0   0   0  |
      13 |  0   0   0   0   0   0  |
      14 |  0   0   0   0   0   0  |
      15 |  0   0   0   0   0   0  |
      16 |  0   0   0   0   0   0  |
      17 |  0   0   0   0   0   0  |
      18 |  0   0   0   0   0   0  |
      19 |  0   0   0   0   0   0  |
      20 |  0   0   0   0   0   0  |
         +-------------------------+
    
    : end
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Last edited by John Gallis; 13 Feb 2017, 14:24.

    Comment


    • #3
      You can more directly write

      Code:
      pmt[7,combn_mat[7,]]=J(1,3,1)

      Comment

      Working...
      X