0
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

You can remove complex elements as follows:

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

then generate desired list:

fun1/@redtab
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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