I have two huge matrices m1 and m2 (length 25000), and for simplicity let us assume they are:
m1 = {{1, 1, -56}, {1.3, 2, 3.06}, {2, 0, -30.02}, {3,
1, -7.291}, {3.5, 2, 1.93}, {4, 0, 0}, {5, 0, 0}, {5.5,
1, -356.4}, {6, 1, 9.945}, {7, 0, -7.512}};
m2 = {{1, 1, -56}, {1, 2, 3.06} + .2, {2,
0, -30.02}, {3, 1, -7.291} + .3, {3, 2, 1.93}, {4, 0, 0}, {5, 0,
0}, {5, 1, -356.4} + .4, {6, 1, 9.945}, {7, 0, -7.512}};
I wrote the following code with the help of this forum to pick those rows of both matrices that have the same first value. However, the code is very slow
picker1 = m1[[All, 1]];
picker2 = m2[[All, 1]];
newM1 = Cases[m1, x_ /; MemberQ[picker2, x[[1]]]]
newM2 = Cases[m2, x_ /; MemberQ[picker1, x[[1]]]]
I know the code is not an efficient and I appreciate if you can help me with more efficient and fast code.
Pick[m1, MemberQ[picker2, #] & /@ picker1]andPick[m2, MemberQ[picker1, #] & /@ picker2]? – kglr Apr 26 '18 at 22:17MemberQhas certain problems with floating point numbers. Do the first entries in each row in your huge matrices happen to be integers? – Henrik Schumacher Apr 26 '18 at 22:25