Announcement

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

  • repeat vector

    Hi
    I was quite sure I've seen a way to repeat vectors in mata the same way as in R with:
    Code:
    > rep(c(0,1), 5)
     [1] 0 1 0 1 0 1 0 1 0 1
    I searched for a while without luck, but then I figured it out:
    Code:
    . mata J(1, 4, (0,1))
           1   2   3   4   5   6   7   8
        +---------------------------------+
      1 |  0   1   0   1   0   1   0   1  |
        +---------------------------------+
    For other lost soules I leave my findings.
    The J function is quite powerfull.
    Have a nice weekend
    Kind regards

    nhb

  • #2
    See also http://www.stata-journal.com/sjpdf.h...iclenum=pr0043

    Comment


    • #3
      One can also expand (eg 4 times) a set of values ("a", "b") by combining the J function with the vec function:
      Code:
      . mata vec(J(4,1, ("a",  "b")))'
             1   2   3   4   5   6   7   8
          +---------------------------------+
        1 |  a   a   a   a   b   b   b   b  |
          +---------------------------------+
      Kind regards

      nhb

      Comment


      • #4
        Niels Henrik Bruun it doesn't generalize to different types just yet, but I put something together like this to handle "recycling" colors in brewscheme. You can view the source in the repository. Similar to the R functionality it will continue to repeat the pattern of values from beginning to end until matching the appropriate length. I suppose I could make it a bit more generalizable by using transmorphic matrix parameters, but I didn't really spend to much time on it after I was able to get what I needed done.

        Comment

        Working...
        X