I'm going to construct a vector "myvec". Each element of "myvec" will come from either "vecA" or "vecB" according to a the selection vector, "sel":
My current solution to this is very clumsy:
So I wanted to ask if there is some more natural way to do this in Mata. As is, I think I may end up tossing my clumsy code into a function...though it will get messier if/when I want to select from more than two vectors. Any suggestions would be appreciated. Thanks!
(I'm used to such a thing in R, which has a vectorized version of Mata's cond ? ifyes : ifno as well as a matrix subsetting syntax (vecA \ vecZ)[...] that would do the trick here.)
Code:
vecA = ("a","b","c") vecZ = ("x","y","z") p = (.5,.5) rseed(1) sel = rdiscrete(1,n_pl,(.5,.5)) // 2,1,2 // desired answer: x,b,z
Code:
len = length(vecA) myvec = (vecA,vecZ)[(1..len):+len:*(sel:-1)]
(I'm used to such a thing in R, which has a vectorized version of Mata's cond ? ifyes : ifno as well as a matrix subsetting syntax (vecA \ vecZ)[...] that would do the trick here.)
Comment