# Table with condition

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?

• Table[] with If[] and Nothing ought to do it. Commented Jul 25, 2016 at 1:42

Table[{a, b}, {a, 16, 65}, {b, 10, Min[16, a - 5 - 1]}] // Flatten[#, 1] &

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

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];


Use Pick

Block[{t=Tuples[{A,B}]},Pick[t,-UnitStep[Apply[Subtract,t,{2}]+5],1]]

• This does not appear to use Table, as requested in the question. Commented Jul 25, 2016 at 4:01
• @bbgodfrey oh, yes......
– Wjx
Commented Jul 25, 2016 at 4:05