Announcement

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

  • vector subscripting utility functions

    Hi Y'All -- This is probably useless to you all, but just in case: I have recently written 3 functions for vectors having to do with subscripting / positions / locations / indices. The first one takes a boolean (1/0) vector same length as x as returns a vector of the positive positions in x. The second one takes a boolean (1/0) vector same length as x and replaces them with a given scalar value or a vector of values equal in length to the number of positives in the boolean position vector. The third one takes x and a vector (called table) of possible values components of x could take and returns a vector of positions in table for each element of x. This is like the match() function in R.

    I am sure these are "beta" at best at this point in time! But, if anyone wants to test drive, pls LMK. Happy New Year. -- Paul

  • #2
    Hi Paul, welcome to the forums and thank you for offering to share your code. I encourage you to either paste the code here for others who might need/want it, or else throw it up on a Github (or similar) repository and share a link.

    Mata is a big language and I am by no means an expert. You might be interested to know that the first two functions you describe can be accomplished with one-liners in Mata. I don't mean to share this to discourage or denigrate your work, only to bring it to your attention if you hadn't been aware of it. I show the code below.

    Code:
    mata:
      X = 0..5
      select(X, (0,1,1,0,1,0))
      X[.,select(X, (0,1,1,0,1,0))]
    end
    As for the last one, matching values against a look up table, I'm not aware of a function that will do that (perhaps it's already in Ben Jann's -moremata- package).

    Again, congrats on working with Mata and on your spirit of open-source sharing.

    Comment


    • #3
      Hi Leonardo! Thank you. There is no way I would have "guessed" the -select()- function, which is what I think is the key here. I will work with my functions a bit more to see if they are robust before posting anywhere. Thx so much for the exchange. -- Paul

      Comment

      Working...
      X