Announcement

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

  • How to cross join or create elementwise combinations of vectors in Mata

    Dear Statlisters

    Is there a Mata equivalent to Stata's cross function, SQL's cross join or R's expand.grid(), such that if X = [1 \ 2] and Y = [3 \ 4] then Z(X,Y) = [1, 3 \ 1, 4 \ 2, 3 \ 2, 4]? Sorting of rows is not important. (Note: the Mata cross() function does something different.)

    I can imagine a solution that adds rows to a new matrix at each step of a nested loop but is there anything faster or more elegant? The application is that I wish to implement a multiple pass (parallel) blocking strategy in probabilistic record linkage. I wish to first create a key of comparisons to be made which can then be deduplicated before deriving the match weights. I'm aiming to avoid the redundant calculations that are created by multiple pass blocking strategies.
    James C. Doidge
    Research Associate
    Centre for Population Health Research
    University of South Australia

  • #2
    James C Doidge --

    I have encountered this sort of problem before, and the best I have been able to do is use the Kronecker and J() operators to get the result. So, for example:

    Code:
    mata:
    X = 1 \ 2
    Y = 3 \ 4
    Z = X#J(rows(Y), 1, 1), J(rows(X), 1, 1)#Y
    end
    Gives something like what you want, I think!

    Matthew J. Baker

    Comment

    Working...
    X