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.

This question already has an answer here:

Let's consider two tables given by

T1 = Table[{i,j,f1[i,j]},{i,0,2},{j,0,3}];
T2 = Table[{i,j,f2[i,j]},{i,0,2},{j,0,3}];

The functions f1 and f2 are expensive to compute, so that I want to compute the tables T1 and T2 only one time. I now want to obtain the table T3 defined as

T3 = Table[{i,j,f1[i,j] f2[i,j]},{i,0,2},{j,0,3}];

However, as I already know T1 and T2, I want to estimate T3 without any reevaluation of f1 and f2, but using only operations on the already known tables T1 and T2.

How would you perform such an operation on the lists ?

share|improve this question

marked as duplicate by Mr.Wizard Apr 24 at 6:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers 2

up vote 2 down vote accepted

Very simply:

T3 = T1;

T3[[All, All, 3]] *= T2[[All, All, 3]];

T3

Reference Part and TimesBy.

Although I strongly prefer the method above over it*, simply as an illustration of other methods you could also use Join and Apply, along with a Function using Slot:

Apply[{#, #2, #3*#6} &, Join[T1, T2, 3], {2}]

* The first reason to prefer the first method is that it will not unpack a packed array, therefore with packed numeric data it can be much faster than alternatives such as Apply or Replace. The second reason, which I outlined in my answer to the marked duplicate, is the consistency of using Part for these operations. Third is the ability to make in-place modifications when desired, saving memory.

share|improve this answer
    
Anyone who wishes to add another method to this Wiki answer please feel free. –  Mr.Wizard Apr 24 at 6:50
    
Thank you. How would you have handled the simpler case where the tables are given by T1 = Flatten[Table[{i,j,f1[i,j]},{i,0,2},{j,0,3}],1] ? –  jibe Apr 24 at 6:56
    
@jibe Just change the second line to T3[[All, 3]] *= T2[[All, 3]]; Once you understand Part this will make sense. :-) There are many other ways, e.g. what chris just posted, but this one is usually faster, and IMHO easier to read once you are familiar with Part. Please see my answer to the linked (duplicate) question for a longer explanation. –  Mr.Wizard Apr 24 at 7:02
    
@jibe Thanks for the Accept. I added a second method to this answer which is perhaps a little easier to generalize, but which will not perform as well. I still recommend the first method. –  Mr.Wizard Apr 24 at 7:06
    
The duplicate is now in my bookmarks ! ;-) A last question... If I get rid of the ; in the second line T3[[All, All, 3]] *= T2[[All, All, 3]];, Mathematica displays some strange intermediate expressions which do not correspond to the actual content of T3. What is the origin of this display which gives rise to confusion ? –  jibe Apr 24 at 7:24

Well just to offer another option:

  MapThread[{#1[[1]], #1[[2]], #1[[3]] #2[[3]]} &, {T1, T2}, 2]
share|improve this answer
    
chris, there is nothing wrong with this but I find the combination of Slot and Part hard to read. I added another form to the other answer that I find clearer. Perhaps you will find it of interest. –  Mr.Wizard Apr 24 at 7:09
    
I did not know Join had second argument so yes thank you. –  chris Apr 24 at 7:11

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