Re: removing non-numeric elements from the table
- To: mathgroup at smc.vnet.net
- Subject: [mg108060] Re: removing non-numeric elements from the table
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Sun, 7 Mar 2010 05:08:21 -0500 (EST)
- References: <hmqj5p$so5$1@smc.vnet.net>
michael partensky 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 > > This question really comes down to style, because as you say, you could just filter the list with NumericQ. There are various aspects of this code that I like to change: 1) I would only use Quiet where really necessary, and avoid generating error messages unnecessarily. Those messages are there to help debug the code, and if you generate a lot, or just turn them off, you may miss a some serious errors. The error regarding inverse functions is probably best suppressed, but the Part error, which your code also generates, is definitely better avoided. I also suspect that code that generates suppressed errors, probably runs more slowly than equivalent code that avoids doing this. 2) Personally, I don't like variables such as rnd defined by delayed assignment. When you have a lot of code, it can be really hard to determine which variables are actually functions! I would avoid fully capitalised variable names such as Eq, which are reserved for use by Wolfram. Thus I would probably rewrite your code like this: eqResults[] := Quiet[Solve[1/2 (Erf[z/Sqrt[2]] + 1) == 2 Random[] - 1, z]]; Map[#[[2]] &, Flatten[Table[eqResults[], {20}]]] Alternatively, you could suppress the inverse functions message specifically. David Bailey http://www.dbaileyconsultancy.co.uk