8
$\begingroup$

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?

$\endgroup$
  • 2
    $\begingroup$ "Objects of unequal length in Mod cannot be combined." means what it says. Unless your two lists have the same dimensions, it doesn't work. Shorten one, or lengthen the other. $\endgroup$ – J. M. is away Feb 22 '16 at 4:53
  • 2
    $\begingroup$ Do you want the output would be in the form {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$ – JungHwan Min Feb 22 '16 at 5:07
  • 1
    $\begingroup$ @JHM, I was under the impression that Mod was listable. In any case, it seems like his error was about "objects of unequal length" so seems like he was trying to do something like in the latter case. That's why I gave my answer about Outer. :) $\endgroup$ – C. Woods Feb 22 '16 at 5:13
  • $\begingroup$ @JHM Sorry my question was unclear. I've edited it now. I think this fits your first example better, but I'm unsure. $\endgroup$ – Elem-Teach-w-Bach-n-Math-Ed Feb 22 '16 at 6:58
  • 3
    $\begingroup$ @user3363795 the answer by @C.Woods or Map[#,list2]&/@list1 $\endgroup$ – ubpdqn Feb 22 '16 at 7:01
12
$\begingroup$

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.

$\endgroup$
  • 2
    $\begingroup$ This would give all possible combination of Mods, wouldn't it? $\endgroup$ – JungHwan Min Feb 22 '16 at 5:03
  • 6
    $\begingroup$ @JHM, To be fair, the OP could have given an explicit example so that we aren't guessing... $\endgroup$ – J. M. is away Feb 22 '16 at 5:06
  • 1
    $\begingroup$ This got it working! Thanks everyone! $\endgroup$ – Elem-Teach-w-Bach-n-Math-Ed Feb 22 '16 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.