0
$\begingroup$

This question already has an answer here:

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.

$\endgroup$

marked as duplicate by Szabolcs, Mr.Wizard Feb 6 '14 at 23:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ If you have only integers Cases[{3, 5, NaN}, _Integer] would work. Same with Real of course. Or if you work on g: gg = Cases[#, _Real | _Integer] & /@ g $\endgroup$ – Öskå Feb 6 '14 at 16:50
  • 1
    $\begingroup$ This might get complicated and it all depends on how you got these NaNs 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. $\endgroup$ – Szabolcs Feb 6 '14 at 17:19
  • $\begingroup$ There's some info on this here. Generally the best thing to do is try not to get NaNs into Mathematica at all as Mathematica can't reliably handle them. $\endgroup$ – Szabolcs Feb 6 '14 at 17:23
  • $\begingroup$ @Szabolcs wouldn't a Cases be sufficient in all cases? $\endgroup$ – Öskå Feb 6 '14 at 17:25
  • $\begingroup$ @Öskå Unfortunately, no, or at least not if you want a guarantee that nothing will be unexpectedly wrong, and that it will work in all versions and all platforms. If the OP imported these lists form a text file, and this NaN he is referring to is a symbol or a string, then yes. However, if it is a true floating point NaN, i.e. a special floating point value, then Mathematica simply doesn't support it and pattern matching is not reliable with it. $\endgroup$ – Szabolcs Feb 6 '14 at 19:23
0
$\begingroup$

Irrespective of the structure of g, nested lists etc...

gg = g /. NaN->Sequence[];

This replaces all NaNs with an empty Sequence that collapses to nothing.

$\endgroup$
  • $\begingroup$ Note: "NaN"->Sequence[] would work. NaN is meant as a string. $\endgroup$ – Fabio Nov 11 '15 at 11:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.