Message Boards Message Boards

0
|
2854 Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Defining the "iteration" before defining a Table

Posted 3 years ago

Hi,

I have 2 functions f[x] and g[x] defined.

I would like to compare them by displaying some values. So I build two tables :

tbA = Table[f[x],{x,1,4}]

tbB = Table[g[x],{x,1,4}]

Can I define the "iterations" i.e. {x,1,4} before defining the tables, so as I have not to duplicate them? Thanks for help.

POSTED BY: Francois Hurter
4 Replies
Posted 3 years ago

@Rohit Namjoshi , @Gianluca Gorni :

I've been struggling with this as well. Now I understand why I was struggling, and I have two new techniques to use.

POSTED BY: Mike Besso

If you need to compare the values, you may try this:

Table[{f[x], g[x]}, {x, 1, 4}] // TableForm
POSTED BY: Gianluca Gorni

Thank you !

POSTED BY: Francois Hurter
Posted 3 years ago

Hi Francois

Since Table has the HoldAll attribute this does not work

xRange = {x, 1, 4};

Table[x, xRange]
(* Non-list iterator xRange at position 2 does not evaluate to a real numeric value *)

Have to force evaluation

Table[x, Evaluate@xRange]
(* {1, 2, 3, 4} *)

To keep the range separate from the iteration symbol

range = {1, 4};

Table[x, Evaluate@{x, Splice@range}]
(* {1, 2, 3, 4} *)
POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract