I have a long list of lists ...similar to g = {{1,2,3},{4,2,7}, .....{1.2,9}}.
Occasionally, g contains elements like {3,5, NaN}, that include the NaN symbol.
I would like to replace g with a new list, say gg, in which all of the offensive list elements (lists including the NaN symbol) have been removed.
Can someone show me how I can do this?
Background:
I am doing some 2D optical waveguide modeling using COMSOL, and I export postprocessing data over a predefined grid. For example, {x,y,z} gives the (x,y) position in the waveguide, and z might be one component of the electric field (which is represented by a complex number). The NaN values come up because COMSOL defines the field outside the WG structure as undefined and some grid positions are just outside the WG structure; those coordinates have NaN for their z-element. So I think it is fine to simply drop those list elements from consideration.
Cases[{3, 5, NaN}, _Integer]would work. Same withRealof course. Or if you work on g:gg = Cases[#, _Real | _Integer] & /@ g– Öskå Feb 06 '14 at 16:50NaNs in the first place. Can you update your question and explain how the NaNs got in your list? They might or might not be symbols. Mathematica itself should never ever give you any NaNs, so usually they appear only if you interface with external programs/libraries through MathLink/LibraryLink. – Szabolcs Feb 06 '14 at 17:19Casesbe sufficient in all cases? – Öskå Feb 06 '14 at 17:25NaNhe is referring to is a symbol or a string, then yes. However, if it is a true floating pointNaN, i.e. a special floating point value, then Mathematica simply doesn't support it and pattern matching is not reliable with it. – Szabolcs Feb 06 '14 at 19:23NaNsymbol. John--it's not possible to answer your question unless you explain where these NaNs came from. – Szabolcs Feb 06 '14 at 19:26NaNvs.NaN'. It's a little infuriating. Especially when you wrap one inRationalizeand see all the different results. – kale Feb 07 '14 at 02:57