# List operation on specific elements [duplicate]

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 ?

-

## marked as duplicate by Mr.Wizard♦Apr 24 '14 at 6:50

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.

-
Anyone who wishes to add another method to this Wiki answer please feel free. – Mr.Wizard Apr 24 '14 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 '14 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 '14 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 '14 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 '14 at 7:24

Well just to offer another option:

  MapThread[{#1[[1]], #1[[2]], #1[[3]] #2[[3]]} &, {T1, T2}, 2]

-
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 '14 at 7:09
I did not know Join had second argument so yes thank you. – chris Apr 24 '14 at 7:11