MathGroup Archive 2013

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Table with condition

  • To: mathgroup at smc.vnet.net
  • Subject: [mg130237] Re: Table with condition
  • From: Alexei Boulbitch <Alexei.Boulbitch at iee.lu>
  • Date: Wed, 27 Mar 2013 03:54:36 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net

Dear mathgroup,
I need to start creating list and continue until some condition is not met.
  For example to generate list of random numbers until the value is not 5. 
Yes, it is theoreticaly possible to generate list with sufficient length, and cut it on the right place afterwords, but it is very inefficient way if the computation is much harder than just generation of random numbers.

TakeWhile[Table[RandomInteger[{0, 10}], {15}], # != 5 &]

Using the while cycle is the other way, but I can only print values using that method, but I don't know, how to generate classical list:

r = 0;
While[r != 5, r = RandomInteger[{0, 10}]; Print[r]]

What is the right solution of such a simple problem?

Thanks in advance for any help

Jakub

Hi, Jakub,

This might be the way to do it. The function makeList makes the job, its parameter is your limit list number (in your case it was 5):

makeList[z_Integer] := Module[{f},
   f[x_] := 1;
   NestWhileList[f[#]*RandomInteger[{1, 10}] &, 1, # <= z &] // Most
   ];

Let us try. The following will return the nestedlist whose each next sublist has elements smaller or equal than the next number from the list Range[9]:

makeList /@ Range[9]

{{1}, {1}, {1, 1, 1}, {1, 4}, {1}, {1, 1, 5, 5}, {1, 1, 7, 3, 6, 7, 4,
   7, 3, 6, 7, 2, 7, 4, 7, 6, 6, 6, 7, 2}, {1, 1, 7, 1, 6, 7, 4, 2, 1,
   7, 2, 4, 2, 4, 3, 5}, {1, 1, 7, 8, 8, 8, 1, 7, 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






  • Prev by Date: Plotting a transition function
  • Next by Date: Re: Creating matrix from another matrix using column position
  • Previous by thread: Re: Table with condition
  • Next by thread: Creating matrix from another matrix using column position