- #1
jackmell
- 1,807
- 54
I notice there are some functions in Mathematica in which you can't supply directly, an array of arguments but rather must give it a list. For example, GCD and LCM. I can use for example, GCD[2,3,8] and it returns 1. However if I code: GCD[{2,3,8}], the function call fails. I know of one way to correct this problem by using Apply, for example, Apply[GCD,{2,3,8}]. Intersection works this way also. However, I'm having problems implementing this with some code I'm writing with a list of lists. I would like to supply to the function, a list of lists and have it compute the intersections of members {a,b} such that a is from the first list and b from that second, then generalize this for a variable number of sublists. For example, suppose my list is:
myList={{{1,2},{3,4},{5,6}},{{7,1},{8,3},{9,5}}}
So that's 2 lists in myList and I I want to compute:
Intersection[{1,2},{7,1}], Intersection[{1,2},{8,3}],Intersection[{1,2},{9,5}]
Intersection[{3,4},{7,1}], Intersection[{3,4},{8,3}],Intersection[{3,4},{9,5}]
Intersection[{5,6},{7,1}], Intersection[{5,6},{8,3}],Intersection[{5,6},{9,5}]I realize I could just hard-code a loop but it would get messy because my list would have a variable number of sub-lists. I'm sure there is a compact way in a single-line command to formulate all those intersections for a variable number of sub-list in myList. However I just cannot figure it out.
Here's what I have so far but I must explicitly code each sublist and I'd like a way to avoid doing that since the number is variable so I'm hoping someone could help me:
Ok thanks for reading,
Jack
myList={{{1,2},{3,4},{5,6}},{{7,1},{8,3},{9,5}}}
So that's 2 lists in myList and I I want to compute:
Intersection[{1,2},{7,1}], Intersection[{1,2},{8,3}],Intersection[{1,2},{9,5}]
Intersection[{3,4},{7,1}], Intersection[{3,4},{8,3}],Intersection[{3,4},{9,5}]
Intersection[{5,6},{7,1}], Intersection[{5,6},{8,3}],Intersection[{5,6},{9,5}]I realize I could just hard-code a loop but it would get messy because my list would have a variable number of sub-lists. I'm sure there is a compact way in a single-line command to formulate all those intersections for a variable number of sub-list in myList. However I just cannot figure it out.
Here's what I have so far but I must explicitly code each sublist and I'd like a way to avoid doing that since the number is variable so I'm hoping someone could help me:
Code:
myList = {{{1, 2}, {3, 4}, {5, 6}}, {{5, 7}, {3, 8}, {9, 6}}};
myOuter = Outer[Intersection, myList[[1]], myList[[2]], 1]
{{{}, {}, {}}, {{}, {3}, {}}, {{5}, {}, {6}}}
Ok thanks for reading,
Jack
Last edited: