Message Boards Message Boards

0
|
4973 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:
GROUPS:

addressing an Output (or input)

Posted 10 years ago

Hi there!

I do like the fact that you can address an Output in mathematica, and use it in expressions. For a simple example, if my input In[1] is

2+2

obviously I get the output Out[1]

4

I can now address this in later calculations, e.g. the input In[2]

Sqrt[Out[1]]

gives as Out[2]

2

.

However, when I go back, and change In[1], for instance to

2+7

I get now Out[3]

9

and simply re-executing the next line

Sqrt[Out[1]]

would obviously still give

2

. I would have to change the line to

Sqrt[Out[3]]

to get

3

This destroys for me the whole sense of being able to address Outputs in Mathematica. It would be way more comfortable to have the lines change automatically, as an output changes, just as it is for example with equation labels in Maple.

Is there a way to do so in Mathematica as well?

Thanks! Gernot

POSTED BY: Gernot H
4 Replies

You might want to use Dynamic.

In[1]:=     a = 4*4

In[2]:=   Dynamic[Sqrt[a]]

If you then change a, it will automatically change the square root as well. You don't even need to re-evaluate. I think that the whole point of the Out[3] labelling is that it gives a unique identifier to every output. If you want content to change dynamically, when you change values in the notebook, the preferred construct seems to by Dynamic.

Cheers, Marco

POSTED BY: Marco Thiel
Posted 10 years ago

Not 100% what I was looking for, but helpful for sure, thanks!

POSTED BY: Gernot H

You may already know this, but you can use the % sign to address the last output. For example,

Sqrt[%]

will always evaluate to the square root of the last output.

POSTED BY: Tim Mayes

The metaphor for In[n] and Out[n] should be thought of as the inputs and outputs sequential in time, not line, linearly from the top of a notebook to the bottom. It is, one way of thinking, a potential confusion in the documentation for how In and Out are often described

?In

In[n] is a global object that is assigned to have a delayed value of the n^th input line.  >>

Although n is a "line number" parameter here, it in reality is a time parameter in "ticks of a clock' where the clock ticks once each time you execute an input.

One could create another tag for cells that is unique to the cell and which behaves the way you wish. It requires uniquely tagging a cell and using that to keep track. There is an option to notebooks that can help: CreateCellID. And the parameter $PreRead may play a part. If I get the time I may put something like this together.

POSTED BY: David Reiss
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