Dear Listers --
I am trying to create a fast function that outputs all of the the common elements in two vectors. Currently, I have code that works as follows:
Running:
Produces the numbers 2,4, and 5. While the function thus does exactly what I would like it to - it returns the common elements of the vectors Z and Y - it requires looping over all the elements of Z. My problem is that in settings where Z has thousands or even hundreds of thousands of elements, looping over all of the entries in Z can be extremely costly in terms of computational time. In fact, in my application, this turns what might be an n^2 running time algorithm into more or less an n^3 one. So, I was wondering: does anyone know how to get the common values without looping over all the entries of one or the other vector?
Best,
Matt Baker
I am trying to create a fast function that outputs all of the the common elements in two vectors. Currently, I have code that works as follows:
Code:
mata: real colvector CommonVals(real colvector Z, real colvector Y) { List=J(0,1,.) for (i=1;i<=rows(Z);i++) if (any(Y:==Z[i])) List=List \ Z[i] return(List) } end
Code:
X=1\2\3\4\5\6 Y=2\4\5\7 CommonVals(X,Y)
Best,
Matt Baker
Comment