I have a particular list of integers and a matrix whose entries are lists. Say I know that my particular list is a subset of at least some of the elements in the matrix. I want to find an efficient way to find the positions of all of the elements of the matrix of which my list is a subset.
Right now, I just loop through all of the entries in the matrix and keep track of which ones entirely contain my list (M is the matrix I want to search through and mylist is my list of integers):
Matches[mylist_] := (
match = {};
Do[Do[If[Intersection[M[[i, j]], mylist] == mylist, match = Append[match, {i, j}]]
,{j,Length[M[[i]]]}];
,{i,Length[M]}];
match);
As an example of my desired output:
M = {{{1, 2, 3}, {1, 2, 4}}, {{5, 4, 3}, {1, 2, 1}}};
Matches[{1, 2}]
(*={{1, 1}, {1, 2}, {2, 2}}*)
This function works how I want it to, but in the code I'm working on I wind up using it thousands of times with matrices and lists containing many more elements. Calling it so many times becomes the limiting factor in how quickly my code runs. Are there any ways to accomplish this that would be more efficient?
I've tried using Position:
Matches2[mylist_]:=(Position[M, _?(Intersection[#,mylist]==mylist&)]);
Using Intersection in a pattern like this generates errors that I don't know how to correct, though, and I'm not sure whether this approach would be faster to begin with. If anyone knows of any more efficient ways to do this, it would be greatly appreciated. Thanks!