0

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.

John
  • 11
  • 1
  • 3
  • 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 – Öskå Feb 06 '14 at 16:50
  • 1
    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. – Szabolcs Feb 06 '14 at 17:19
  • 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. – Szabolcs Feb 06 '14 at 17:23
  • @Szabolcs wouldn't a Cases be sufficient in all cases? – Öskå Feb 06 '14 at 17:25
  • @Ö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. – Szabolcs Feb 06 '14 at 19:23
  • @Öskå Another problem is that you can't type a NaN directly into Mathematica---as I said it gives you no way to directly generate these values unless you inject them through a low-level API like MathLink/LibraryLink. They can be manipulated only if you store them into a variable, but not typed directly as input. So if the OP encountered a true floating point NaN, then none of the answers below will work because they all try to replace a NaN symbol. John--it's not possible to answer your question unless you explain where these NaNs came from. – Szabolcs Feb 06 '14 at 19:26
  • @Szabolcs thanks for taking the time to explain :) – Öskå Feb 06 '14 at 19:36
  • 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. – John Feb 06 '14 at 21:11
  • @John Thanks for the additional details, but what I need to know is not why, but how do you get those NaNs into Mathematica? Do you use Import to read them from a file? If yes, what type of file? We need to know if those NaNs are symbols, strings, or floating point numbers to be able to give you a usable answer. – Szabolcs Feb 06 '14 at 23:32
  • @Szabolcs It's the dreaded NaN vs. NaN'. It's a little infuriating. Especially when you wrap one in Rationalize and see all the different results. – kale Feb 07 '14 at 02:57

1 Answers1

0

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.

Ymareth
  • 4,741
  • 20
  • 28