If I understand the original posting correctly, what you are trying
to carry out is a true match merge [using the terminology of the
Stata manual entry for -merge-] with general values of the vector and
matrix. Assuming that is correct, then Mata does not provide any
direct way of doing this. Then, the question is how general you want to be.
A sketch of some special purpose code for vector v and matrix M would
be as follows:
1. Use -sort()- to row-sort both M and a matrix V consisting of v
augmented by a column of original row numbers.
2. Loop through V & M to match rows identified by the element in the
first column of each, thus creating V_M.
3. Decide how to treat non-matched rows in V & M and cases with
4. Finally, re-sort V_M on the row indices of v in column 2 and drop
None of this is difficult - provided that you are clear in dealing
with non-matches and other special cases - and it can be easily
implemented as a Mata function.
An alternative would be to write v & M to temporary Stata datasets
and use -merge- to join them. This will certainly be more
expensive. It may or may not be simpler from your perspective,
depending upon how you are using Mata - e.g. you would not want to do
this when programming a stand-alone Mata function or procedure.