Re: How to get elements satisfying specific condition from a list
- To: mathgroup at smc.vnet.net
- Subject: [mg123405] Re: How to get elements satisfying specific condition from a list
- From: Alexei Boulbitch <Alexei.Boulbitch at iee.lu>
- Date: Tue, 6 Dec 2011 03:11:50 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
Hi, please help me if you can.. Let
B:= Table[{x, y}, {x, 1, 6}, {y, 1, 6}]
It is clear that B has 36 elements.
I want to get the list of elements satisfying
'the first component + second component is bigger than 9'
so that the answer is
{{5,5},{5,6},{6,5},{6,6}}.
In fact, I have no idea for following even simpler problem. : Let A be a set of all natural numbers less than 10. Find every element whose squre root is bigger than 2. (needless to say the answer is {5,6,7,8,9})
Can you help me ?
Yes, we can.
First the answer to your second question: use Select in such cases
A = Range[10]
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Select[A, Sqrt[#] > 2 &]
{5, 6, 7, 8, 9, 10}
Concerning the first question. First of all your initial list is too deep:
B = Table[{x, y}, {x, 1, 6}, {y, 1, 6}]
{{{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}}, {{2, 1}, {2,
2}, {2, 3}, {2, 4}, {2, 5}, {2, 6}}, {{3, 1}, {3, 2}, {3, 3}, {3,
4}, {3, 5}, {3, 6}}, {{4, 1}, {4, 2}, {4, 3}, {4, 4}, {4, 5}, {4,
6}}, {{5, 1}, {5, 2}, {5, 3}, {5, 4}, {5, 5}, {5, 6}}, {{6, 1}, {6,
2}, {6, 3}, {6, 4}, {6, 5}, {6, 6}}}
You should get rid of extra parentheses, then again Select will do the job:
B1 = Flatten[Table[{x, y}, {x, 1, 6}, {y, 1, 6}], 1]
{{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {2, 1}, {2, 2}, {2,
3}, {2, 4}, {2, 5}, {2, 6}, {3, 1}, {3, 2}, {3, 3}, {3, 4}, {3,
5}, {3, 6}, {4, 1}, {4, 2}, {4, 3}, {4, 4}, {4, 5}, {4, 6}, {5,
1}, {5, 2}, {5, 3}, {5, 4}, {5, 5}, {5, 6}, {6, 1}, {6, 2}, {6,
3}, {6, 4}, {6, 5}, {6, 6}}
Select[B1, #[[1]] + #[[2]] > 9 &]
{{4, 6}, {5, 5}, {5, 6}, {6, 4}, {6, 5}, {6, 6}}
Have fun, Alexei
Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG
Office phone : +352-2454-2566
Office fax: +352-2454-3566
mobile phone: +49 151 52 40 66 44
e-mail: alexei.boulbitch at iee.lu<mailto:alexei.boulbitch at iee.lu>