MathGroup Archive 2009

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

Search the Archive

Re: defining consecutive variables

  • To: mathgroup at smc.vnet.net
  • Subject: [mg99479] Re: [mg99392] defining consecutive variables
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Wed, 6 May 2009 05:27:18 -0400 (EDT)
  • Reply-to: hanlonr at cox.net

Use variable names of the form q[20] rather than q20. Much easier to reference and program

Table[q[n] = 
   RandomReal[{0, n}, {5, 5}], {n, 0, 40}];

Total[q[20]/5]

{13.4727,3.49786,8.55356,13.1667,10.2449}

Total /@ q[20]/5

{5.14601,10.0176,9.42529,12.5766,11.7702}

Table[Mean[Flatten[q[n]]], {n, 0, 40}] ==
 
 Mean /@ Flatten /@ Last /@ DownValues[q]

True


Bob Hanlon

---- Jason <jbiggs2 at uoregon.edu> wrote: 

=============
I have a code where I need to define a large number of variables as matrices, called q1,q2,q3......qn. I'd like to be able to define them all without writing out n assignment lines, so a Do loop seems appropriate to me but I don't know how to assign sequential variable names. This gets the job done but it is really ugly IMO


f[x_] := Table[x RandomReal[], {n, 5}, {np, 5}](*for example*)

Do[ToExpression["q" <> ToString[n] <> "=f[n]"], {n, 0, 40}]

at the end of which I have 41 matrices which I can call as q0,q1, etc. Is this the best way to accomplish this task?

Thanks



  • Prev by Date: Re: Reading csv with ;
  • Next by Date: Re: programmatically rotating a function plot
  • Previous by thread: Re: defining consecutive variables
  • Next by thread: Re: defining consecutive variables