Announcement

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

  • Loop over random sequence of numbers

    Hi all,
    this is probably very basic question, but i couldn't find an answer on internet or help files. How can i make a loop in mata over some random seqence of numbers for example (2,4,67,5,888,44)
    I realize i can create vector and loop over its values, but still going over ordered sequence, like
    Code:
    mata
    c = (2,33,4,5,67,8)
    
    for (i=1; i<=length(c); i++) {
    g = c[1,i]
    g
    
    }
    end
    but is there something more direct like stata foreach v in .... ?
    Thanks!

  • #2
    If your vector contains numbers in a random order, then going through that vector *would* involve looping a random sequence of numbers. Is your problem "how do I generate a random sequence of numbers over which to loop?" If so, see runiform() or runiformint(). If that is not relevant, I'd need some further information from you. Also, I don't understand in what sense the loop you show is not "direct" as compared to -foreach-.

    Comment


    • #3
      Thanks for reply! I was thinking something like stata loop

      Code:
      foreach x of numlist 33 4 6 77 8 {
      di `x' 
      }
      so in my above example, i would have value "i" not necessarily ordered or values with regular increments.

      Comment

      Working...
      X