2
$\begingroup$

I want to list {a,c} but c is depend on b, suppose the dependence on b can not written as a simple function c(b). How can I create a list of {a,c} list without list out b in the intermediate.

My workaround is this:

list = Table[{a, b = a^2, c = b^2}, {a, 0, 10, 1}];
listWanted = list[[All, {1, 3}]]

However suppose the intermediate variable is many, doing this is cumbersome, at least not neat. I just want to calculate these intermediate value but not list them.

$\endgroup$
6
  • $\begingroup$ Obviously it depends on what you're actually doing (this might be too minimal an example), but you can certainly do list = Table[{a, (a^2)^2}, {a, 0, 10, 1}]. $\endgroup$
    – march
    Commented Oct 26, 2015 at 3:41
  • 3
    $\begingroup$ ...or, Table[{a, b = a^2; c = b^2}, {a, 0, 10, 1}]. $\endgroup$ Commented Oct 26, 2015 at 3:49
  • $\begingroup$ @J.M. This is just what I want, thanks! $\endgroup$ Commented Oct 26, 2015 at 6:18
  • $\begingroup$ @march I've said that c is can not be written as a simple function of a $\endgroup$ Commented Oct 26, 2015 at 6:19
  • $\begingroup$ Did you understand what I replaced? If so, please write an answer to your own question. :) $\endgroup$ Commented Oct 26, 2015 at 6:22

1 Answer 1

2
$\begingroup$

One can using the ; operator to do the evaluation, but suppress the output. So the code can be the following as pointed out in the comment:

Table[{a, b = a^2; c = b^2}, {a, 0, 10, 1}]
$\endgroup$
2
  • 1
    $\begingroup$ Maybe you already know this, but chances are you don't: there is more to ; than one might expect! $\endgroup$
    – sebhofer
    Commented Oct 26, 2015 at 8:57
  • $\begingroup$ @sebhofer thank foe the link, can you explain this code using the principal you linked? $\endgroup$ Commented Oct 26, 2015 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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