3
$\begingroup$

I have 2 lists of numbers A = Range[16, 65] and B = Range[10, 16]. I want to form all possible combinations $(a,b)$ of the elements in those lists such that $a - b > 5$. I can get what I want with:

Tuples[{A, B}] /. {a_, b_} /; a - b <= 5 -> Nothing

How can I get the same list using Table?

$\endgroup$
7
$\begingroup$
Table[{a, b}, {a, 16, 65}, {b, 10, Min[16, a - 5 - 1]}] // Flatten[#, 1] &
$\endgroup$
  • $\begingroup$ rather Table[{a, b}, {a, 16, 65}, {b, 10, Min[16, a - 5]}] // Flatten[#, 1]&... $\endgroup$ – amrods Jul 25 '16 at 1:56
  • $\begingroup$ @amrods Thanks! But I think it should be Min[16, a - 5 - 1] because a - b > 5 means b <= a - 6 $\endgroup$ – wuyingddg Jul 25 '16 at 2:04
4
$\begingroup$

To do it with Table modify your Tuples example:

Flatten[Table[If[a - b > 5, {a, b}, Nothing],
   {a, Range[16, 65]}, {b, Range[10, 16]}], 1];

If you are not committed to Table another idea is to use Outer

Flatten[Outer[If[#1 - #2 > 5, List[#1, #2], Nothing] &, 
   Range[16, 65], Range[10, 16]], 1];
$\endgroup$
0
$\begingroup$

Use Pick

Block[{t=Tuples[{A,B}]},Pick[t,-UnitStep[Apply[Subtract,t,{2}]+5],1]]
$\endgroup$
  • 2
    $\begingroup$ This does not appear to use Table, as requested in the question. $\endgroup$ – bbgodfrey Jul 25 '16 at 4:01
  • $\begingroup$ @bbgodfrey oh, yes...... $\endgroup$ – Wjx Jul 25 '16 at 4:05

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.