Announcement

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

  • make indicator matrix in mata

    Hi, i want to make a 0/1 matrix in mata based on some other matrix (in my example W) based on value of each element, so if it is greater than some value element should be 1 and otherwise 0.I can make it by loops as below but is there some command that already does this in shorter way?Thanks!

    mata:
    n = rows(W)

    for (i=1; i<=n; i++){
    for (j=1; j<=n; j++){
    if (W[i,j]>2) W[i,j] = 1
    else W[i,j] = 0
    }
    }
    end

  • #2
    tratt (please see the FAQ about the preference on this forum for real given and family names. Thanks):
    ​this query should be posted to Mata forum.
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Exactly as Carlo said, but any way

      Code:
      : W = (1, 2, 3 \ 3, 2, 1 \ 1, 2, 3)
      
      : W :> 2
             1   2   3
          +-------------+
        1 |  0   0   1  |
        2 |  1   0   0  |
        3 |  0   0   1  |
          +-------------+
      
      : frog = W :> 2
      
      : frog
             1   2   3
          +-------------+
        1 |  0   0   1  |
        2 |  1   0   0  |
        3 |  0   0   1  |
          +-------------+

      Comment


      • #4
        Thanks!

        Comment

        Working...
        X