Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am working with a way to generate a table of some numbers, but the one functions could return the complex numbers which I don't want. I am going to filter out the complex numbers. The only way I can think of is to use a loop to copy the desired numbers from one list to another

fun1[{a_, b_, c_}] := {a, a*b + c, c};
fun2[a_, b_] := Sqrt[a - a/b];
pval = Table[{2, fun2[3, i], i}, {i, -0.5, 1.5, 0.15}];

pval2 = {};
For[k = 1, k <= Length[pval], k++, 
  If[SameQ[Head[pval[[k, 2]]], Complex] == False, 
     pval2 = Insert[pval2, pval[[k]], Length[pval2] + 1]]]
pval = fun1 /@ pval2

In this code, fun2 might return real or complex number. pval is a table with each elements a list of 3 numbers, the second number of each table element is returned by fun2. I don't want to keep any elements contains complex numbers.

Above code is just an example. In the practical case, the functional form of fun1 and fun2 will be more complicated.

share|improve this question
1  
take a look here mathematica.stackexchange.com/q/40169/5478 –  Kuba Jan 13 at 21:33
    
thanks a lot. It is so neat and helps :) –  user1285419 Jan 13 at 21:50
add comment

1 Answer 1

up vote 1 down vote accepted

You can remove complex elements as follows:

redtab=Cases[pval, Except[{_, Complex[_, _], _}]];

then generate desired list:

fun1/@redtab
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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