This feels like a super basic question, but how do I find the intersection of two matrices in mata? Say I have two matrices A and B
What is then the best way to find the elements that occur in A and B?
Is the only way to do this to loop over all values of one matrix and see if selectindex() produces anything?
PS: Some additional context. I want to do a heckman selection model with twoway clustered standard errors. I currently do this by running the Heckman three times and recalculating the variance matrix (V_twoway = V1 + V2 - V1&2). Unfortunately, I have a couple dozen million observations and not the best external instrument, so running the Heckman once can take days (187 iterations). Running it three times then isn't very appealing. Instead, I want to calculate the twoway clustered variance matrix directly from the Xs and residuals. At some point in this process I need to determine if an observation is part of both cluster 1 and cluster 2. I can determine the separate elements using selectindex(), but I'm struggling how to then determine that they are present in both.
PS2: I suppose the question could be rephrased as, "how to do a select with two if-conditions".
Code:
A = (1, 2, 3) B = (2, 3, 4)
Code:
(2, 3)
PS: Some additional context. I want to do a heckman selection model with twoway clustered standard errors. I currently do this by running the Heckman three times and recalculating the variance matrix (V_twoway = V1 + V2 - V1&2). Unfortunately, I have a couple dozen million observations and not the best external instrument, so running the Heckman once can take days (187 iterations). Running it three times then isn't very appealing. Instead, I want to calculate the twoway clustered variance matrix directly from the Xs and residuals. At some point in this process I need to determine if an observation is part of both cluster 1 and cluster 2. I can determine the separate elements using selectindex(), but I'm struggling how to then determine that they are present in both.
PS2: I suppose the question could be rephrased as, "how to do a select with two if-conditions".
Comment