Re: removing non-numeric elements from the table
- To: mathgroup at smc.vnet.net
- Subject: [mg108028] Re: [mg108016] removing non-numeric elements from the table
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Sun, 7 Mar 2010 04:00:38 -0500 (EST)
- References: <201003050933.EAA29417@smc.vnet.net>
- Reply-to: drmajorbob at yahoo.com
This is a little better, I suppose: rnd := Random[] eq := Module[{soln = Quiet@Solve[1/2 (Erf[z/Sqrt[2]] + 1) == 2 rnd - 1, z]}, If[soln == {}, Unevaluated@Sequence[], z /. First@soln] ] tvR = Table[eq, {20}] {1.01786, 0.700284, 1.31507, -0.0451832, -0.351896, 0.630807, \ -0.653897, 0.467794, 1.54571, 0.103691, 0.70144} and now this can also happen, sometimes: tvR = Table[eq, {1}] {} Here's a better way, and how I arrived at it. First, solve the equation in general: Clear[z] z[r_] = z /. First@Quiet@Solve[1/2 (Erf[z/Sqrt[2]] + 1) == 2 r - 1, z] Sqrt[2] InverseErf[-3 + 4 r] Next, look up InverseErf in Help, where we find that InverseErf[s] is defined only for -1 <= s <= 1. What values of r are allowed, then? Reduce[-1 <= -3 + 4 r <= 1] 1/2 <= r <= 1 So... your original Solve will succeed when Random[] is at least 1/2. Otherwise it will fail, and you want to ignore all the failures. Hence, this seems like better code: Clear[z] z[r_] = z /. First@Quiet@Solve[1/2 (Erf[z/Sqrt[2]] + 1) == 2 r - 1, z]; better := If[# >= 1/2, z@#, Unevaluated@Sequence[]] &@Random[] Table[better, {20}] {0.515755, 0.165058, -1.19993, -0.0950513, -0.31622, 1.07358, \ 0.779315, 0.972604, -0.58977} or... somewhat simpler: simple := If[# >= 1/2, Sqrt[2] InverseErf[-3 + 4 #], "delete"] &@Random[] Cases[Table[simple, {20}], _?NumericQ] {1.79536, 1.71484, 0.483297, -0.439203, 0.248217, -1.45833, -1.47775, \ 1.12188, -0.52423} Bobby On Fri, 05 Mar 2010 03:33:34 -0600, 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 > > -- DrMajorBob at yahoo.com
- References:
- removing non-numeric elements from the table
- From: michael partensky <partensky@gmail.com>
- removing non-numeric elements from the table