Re: removing non-numeric elements from the table
- To: mathgroup at smc.vnet.net
- Subject: [mg108063] Re: [mg108016] removing non-numeric elements from the table
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Sun, 7 Mar 2010 05:08:54 -0500 (EST)
- References: <201003050933.EAA29417@smc.vnet.net>
Hi Michael, First note that your code could be written a little more compactly using nested lists of rules: In[30]:= Quiet[z /. Table[Eq, {20}]] Out[30]= {z, {-0.658661}, z, {-1.33099}, z, z, {0.95508}, {-1.55336}, \ {-0.453457}, z, z, {-0.976592}, {-0.838431}, {-0.116113}, {-1.07435}, \ {0.031166}, {-0.0701658}, z, z, z} You can first make a table of solutions and then apply it and eliminate z: In[31]:= Quiet[DeleteCases[z /. Table[Eq, {20}], z][[All, 1]]] Out[31]= {-0.0250797, 0.0989299, 1.25444, -1.74394, 1.11036, \ 0.569451, 1.70946, -1.48392, 1.45803, -0.878776} Or, which is the same, eliminate empty lists from the list of solutions first, and then apply it: In[32]:= Quiet[Part[z /. DeleteCases[Table[Eq, {20}], {}], All, 1]] Out[32]= {-1.72183, 1.09259, -0.594554, -0.0791054, -0.60679, \ 0.633106, 0.184821, 1.46669, -1.03662, 0.0210264, -0.864565, -0.358463} If you don't want to even produce the empty solution lists, you can do it for example as follows: In[34]:= Quiet[ Part[z /. Table[If[# === {}, Sequence @@ {}, #] &[Eq], {20}], All, 1]] Out[34]= {-0.630328, 0.411173, -1.34137, -1.04662, -0.781298, \ 1.67024, 0.314125, -2.04354, -0.473975, -0.906154, -3.50552, 0.554472} Note that in the above the pure function is necessary to avoid re-evalution of Eq in the last branch of If. Not also that with this method you can not use Sequence[] directly in If, since If is not SequenceHold. By the way, try to not get into the habit of starting your symbols with a capital letter, and also using global variables where essentially functions are needed. This is how I'd rewrite your code: In[45]:= Clear[rnd, eq, tvR, solutions]; rnd[] := Random[]; eq[s_] := Solve[1/2 (Erf[s/Sqrt[2]] + 1) == 2 rnd[] - 1, s]; solutions[eqF_, num_Integer?NonNegative] := Module[{z}, Quiet[DeleteCases[z /. Table[eqF[z], {num}], z][[All, 1]]]]; In[49]:= tvR = solutions[eq, 20] Out[49]= {0.588592, 0.296185, -0.914506, 0.473337, 1.97816, 1.48341, -0.701329, -0.320757} In the case of <rnd>, the change is purely stylistic - to show to the reader that you use <rnd> as a function, albeit taking no arguments. Now you have no chance of accidental collision with some global value that <z> may have. If the above seems too much of a hassle, at the very least I'd wrap the code for tvR in Block[{z},...]. Hope this helps. Regards, Leonid On Fri, Mar 5, 2010 at 1:33 AM, michael partensky <partensky at gmail.com>wrote: > Hi. I am building a large table filled with the numerical solutions of an > equation. > Here is an example, where a normal standard variable is being generated. > > > rnd:=Random[]; > Eq:=Solve[1/2 (Erf[z/Sqrt[2]]+1)==2 rnd-1,z]; > tvR=Quiet[Table[z/.Eq[[1]],{20}]]; > > In[187]:= > tvR > > Out[187]= > {0.761439, z /. {}[[1]], z /. {}[[1]], 1.07111, 0.031784, z /. {}[[1]], z > /. > {}[[1]], 0.808313, -1.04238, z /. {}[[1]], 0.376982, z /. {}[[1]], 1.06622, > z /. {}[[1]], -0.546643, -0.367224, 0.28111, -0.0606866, z /. {}[[1]], z /. > {}[[1]]} > > What is a nicer way of removing the non-numeric elements? Clearly, it is > possible to filter them out using say NumericQ, but I am looking for > something more elegant (it is apart of a lecture), preferably right while > the Table is being generated. > > Thanks > Michael > > >
- References:
- removing non-numeric elements from the table
- From: michael partensky <partensky@gmail.com>
- removing non-numeric elements from the table