Re: How to get elements satisfying specific condition from a list
- To: mathgroup at smc.vnet.net
- Subject: [mg123391] Re: How to get elements satisfying specific condition from a list
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Mon, 5 Dec 2011 05:15:51 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
On 12/4/11 at 2:51 AM, e-changb at hanmail.net (e-changb) wrote: >Hi, please help me if you can.. Let >B:= Table[{x, y}, {x, 1, 6}, {y, 1, 6}] A couple of comments here. It is wiser to use lower case letters for your variables so as to avoid conflicts with built in symbols. Also, no need for SetDelayed (:=) here. Set (=) will do what you want. >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}}. From your description both {4,6} and {6,4} should be included. In any case, what you want can be done as: In[12]:= b = Table[{x, y}, {x, 1, 6}, {y, 1, 6}]; Cases[Flatten[b, 1], _?(Total@# > 9 &)] Out[13]= {{4, 6}, {5, 5}, {5, 6}, {6, 4}, {6, 5}, {6, 6}} Or a little more efficiently as: In[14]:= b = Tuples[Range[6], {2}]; Cases[b, _?(Total@# > 9 &)] Out[15]= {{4, 6}, {5, 5}, {5, 6}, {6, 4}, {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}) This could be done as: In[16]:= Cases[Range[10], _?(2 < Sqrt[#] &)] Out[16]= {5,6,7,8,9,10}