I tried to search for a topic on this, but didn't seem to find anything, so here goes.

I'm pretty new to Mathematica, so there are a lot of things I don't know yet, but one thing is pretty annoying so far.

If I type something, lets say 10 lines with functions, variables, matrices and so on, and I push SHIFT + Enter on each line to get an output. Then I can either give each function, plot etc. a name or some sort, so I can use it always. But I can also, at least I've seen, use %, and then it takes the last output, if I'm not mistaken, or else I can use %(number) to define which output I want to use.

But, and now comes the problem, if I want to change something in a previous line, let's say a value in a matrix needs to be changed, and I SHIFT+Enter that line again to update the output. Then the input/output number changes, and the places where I have used the % command, and want to update as well, are now flawed because the input/output is not the same as before.

As I said, I find it pretty annoying that I just can't change a tiny value without affecting the whole document. But maybe it's just because I'm a noob, and don't know how to do it ?

Thanks in advance.

  • $\begingroup$ Recommend that you evaluate ??In and ??Out to get a better feel for what is going in your session's In/Out history. Best to do this in a notebook that has had a few but not too many evaluations. $\endgroup$ – m_goldberg Mar 8 '14 at 2:46
  • 4
    $\begingroup$ The best advice I can give is to NOT rely on such programming. You'll only waste time fighting the system. It's true, % and friends are good for one-off cases where you're too lazy (or it is unimportant) to assign the output to a variable, but relying on this to do your work will only end in tears... some day. $\endgroup$ – rm -rf Mar 8 '14 at 3:07
  • $\begingroup$ Yes, this does sound like a case of "Doctor, doctor, it hurts when I do this." "So don't do that then." $\endgroup$ – Rahul Mar 8 '14 at 4:59
  • $\begingroup$ % is (mostly, avoid $HistoryLength=0) o.k. as long as you keep code lines in one cell, i.e. evaluate them in one go. Once you start using separate cells, you should not rely on Out too much. $\endgroup$ – Yves Klett Mar 8 '14 at 9:10

There is no way you can avoid the In / Out number increase.

Keep in mind that, even if Mathematica is not showing all the output is still keeping everything in memory. So if instead of using just % you use %n you programs will not break.


As an example:

In[140]:= m = {{1,2},{3,4}}
Out[140]= {{1,2},{3,4}}    
In[141]:= v= {x,y}
Out[141]= {x,y}
In[142]:= m.%
Out[142]= {x+2 y,3 x+4 y}

so far so good. Let's now change m

In[143]:= m = {{1,2},{6,4}}
Out[143]= {{1,2},{6,4}}

now notice how m.% is not working as expected:

In[144]:= m.%
Out[144]= {{13,10},{30,28}}

My suggestion is to write In[142] as

In[145]:= m.%141
Out[145]= {x+2 y,6 x+4 y}

Now you can change m as much as you want and %141 will always reference to {x,y}

  • $\begingroup$ Can you post a small example of what you mean? $\endgroup$ – Ymareth Mar 8 '14 at 1:36
  • $\begingroup$ Done. Let me know if it clearer or need further explanation! $\endgroup$ – mete Mar 8 '14 at 1:59
  • $\begingroup$ Seems nice. But you say that %141 will always reference to v in this case, is that even though the input/output numbers changes in the original 141 ? $\endgroup$ – Denver Dang Mar 8 '14 at 2:12
  • $\begingroup$ Yes, because you are not changing %141. Even if you change v it will use a different number, so that %141 will always reference to the same thing $\endgroup$ – mete Mar 8 '14 at 2:38
  • $\begingroup$ Sorry @mete but your m.% in cell 144 is working as expected. It is equal to m.m where m is defined in cell 143. That is because % refers to the last output cell (the one with the highest number, so far). $\endgroup$ – magma Mar 8 '14 at 11:23

Hi Denver and welcome to Mathematica.SE.I see that this is your first question here. Your description of the working of % and %n and their limitations is correct and unavoidable. I understand you are a novice MMA user so let me tell you how and when an experienced user uses % and %n.

NEVER! ...Well almost never :-)

In pre-Mathematica days people used to make calculations on the "back of an emvelope". Feynman used to sketch his physics intuitions on place mats in a topless bar, but obviously he would later polish and transfer them in a notebook.

Nowadays he would probably bring a small laptop to the bar, do the calculations with MMA, polish them almost immediately, and have them published on the net over WiFi, before leaving the bar.

Would he use % or %n? Certainly not! Otherwise he would run the risk of not being able to get the same results again.

% and %n are very fragile mechanisms which can be disrupted simply by making the calculations in a different order or by re-evaluating the input cells.

If you have any formula that you are going to use again later in the same notebook, give it a name (even if it is a Plot or Graph or another thing you would not normally name in computerless math) with Set (=) and do not worry about % and %n anymore.

This practice will make your notebooks forever readable and re-evaluateable by yourself and other people.

  • $\begingroup$ I will keep that in mind, thank you :) $\endgroup$ – Denver Dang Mar 8 '14 at 12:44

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.