Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  m_goldberg Mar 8 at 2:46
4  
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. –  rm -rf Mar 8 at 3:07
    
Yes, this does sound like a case of "Doctor, doctor, it hurts when I do this." "So don't do that then." –  Rahul Mar 8 at 4:59
    
% 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. –  Yves Klett Mar 8 at 9:10

2 Answers 2

up vote 2 down vote accepted

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.

EDIT:

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}

share|improve this answer
    
Can you post a small example of what you mean? –  Ymareth Mar 8 at 1:36
    
Done. Let me know if it clearer or need further explanation! –  mete Mar 8 at 1:59
    
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 ? –  Denver Dang Mar 8 at 2:12
    
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 –  mete Mar 8 at 2:38
    
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). –  magma Mar 8 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.

share|improve this answer
    
I will keep that in mind, thank you :) –  Denver Dang Mar 8 at 12:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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