Just on my free trial and can't find this anywhere in online docs. Trying to use the Mod function as follows:
Mod[{list},{list}}
where it takes each number in list one mod each number in list two, but I get the error "Objects of unequal length in Mod cannot be combined." For instance, Mod[{55,76},{10,3,7}]
yielding {5,1,6} for 55 and {6,1,6} for 76. Any way to do this?
1 Answer
Yes, you can use the built in function Outer
. It does exactly the kind of thing you are talking about it. Try
Outer[Mod, list1, list2]
Outer
is a generalization of the outer product in Linear Algebra. Its first argument is a function, and the rest of its arguments are lists. Basically, it applies the function in the first argument to every element in the Cartesian product of the rest of the arguments. This is useful for doing things like you are trying to do: e.g. apply this function to every possible combination of these things.
-
2$\begingroup$ This would give all possible combination of
Mod
s, wouldn't it? $\endgroup$ Feb 22, 2016 at 5:03 -
6$\begingroup$ @JHM, To be fair, the OP could have given an explicit example so that we aren't guessing... $\endgroup$ Feb 22, 2016 at 5:06
-
1$\begingroup$ This got it working! Thanks everyone! $\endgroup$ Feb 22, 2016 at 7:36
{Mod[list1[[1]], list2[[1]]], Mod[list1[[2]], list2[[2]]], ...}
or{{Mod[list1[[1]], list2[[1]]], Mod[list1[[1]], list2[[2]]], ...}, {Mod[list1[[2]], list2[[1]]], Mod[list1[[2]], list2[[2]]], ...}, ... }
? Please specify! If the latter is the case, C. Woods' answer would be appropriate. $\endgroup$Map[#,list2]&/@list1
$\endgroup$