Message Boards Message Boards

[✓] Remove/Delete imaginary parts of a list?

GROUPS:

Dear all,

I am struggling with a simple question I cannot handle myself. I currently calculated growth rates using the difference of the logs. The formula looks as follows: N[Log[Flatten[data2014]] - Log[Flatten[data2013]]]; As the vectors include zeros, I get the message Indeterminate expression -\[Infinity]+\[Infinity] encountered. This is no problem as I excluded these values using the DeleteCases formula for Indeterminateand +/- Infinityvalues.

However, I have the problem that the list afterwards still includes imaginary parts as can be seen in the example data 0.357725 +3.14159 I,0.384469, 0.134341, 0.347519, 0.152898, -0.147894, 0.0408252, -0.0786514, 0.00687373, -0.0341954, 0.155977, -0.00830819,0.293424, -0.0261585, 0.0260584, -0.0291487, -0.0291487, -0.0291487, -0.113863, -0.0686648 + 0. I,

Now my question would be: How is it possible to exclude these imaginary components? I searched on the web and also tried to look it up in the Complex Numbers directory, however, unfortunately, I could not find a solution. Hence I would be very pleased if someone of you could help me.

Thank you in advance!

Best regards Alex

POSTED BY: Alexander Hempfing
Answer
2 months ago

By far the easiest is using Cases, but other solutions are possible:

list = {0.357725 + 3.14159 I, 0.384469, 0.134341, 0.347519, 
  0.152898, -0.147894, 0.0408252, -0.0786514, 0.00687373, -0.0341954, 
  0.155977, -0.00830819, 0.293424, -0.0261585, 
  0.0260584, -0.0291487, -0.0291487, -0.0291487, -0.113863, \
-0.0686648 + 0. I}
Cases[list, _Real]
Select[list, Head[#] =!= Complex &]
Select[list, # == Re[#] &] // Chop

Notice the difference between the last and the penultimate way of doing. is 0. I considered complex or not?

POSTED BY: Sander Huisman
Answer
2 months ago

Dear Sander,

thank you very much for your immediate reply! I used your suggestion with the Cases formula, and it worked well, thank you! 0. I is not considered complex.

Best regards Alex

POSTED BY: Alexander Hempfing
Answer
2 months ago

Be mindful that:

Cases[{12.3 + 0. I},_Real]

will delete the entry. Apply Chop before:

Cases[Chop[{12.3 + 0. I}], _Real]
POSTED BY: Sander Huisman
Answer
2 months ago

Group Abstract Group Abstract