Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #6975

Re: How to get elements satisfying specific condition from a list

From Alexei Boulbitch <Alexei.Boulbitch@iee.lu>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: How to get elements satisfying specific condition from a list
Date 2011-12-07 11:14 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jbnhrc$44f$1@smc.vnet.net> (permalink)

Show all headers | View raw


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@iee.lu<mailto:alexei.boulbitch@iee.lu>





Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: How to get elements satisfying specific condition from a list Alexei Boulbitch <Alexei.Boulbitch@iee.lu> - 2011-12-07 11:14 +0000

csiph-web