4
$\begingroup$

I have

J = Table[{x10, y10, x10*y10}, {x10, 0, 1, 0.5}, {y10, 0, 1, 0.5}]
L = Table[{x10, y10, 2.0*x10*y10}, {x10, 0, 1, 0.5}, {y10, 0, 1, 0.5}]

I want the third elements of J and L to be added and the first and second elements are as they are (as they are the same in both cases) for an example.

$\endgroup$
  • 4
    $\begingroup$ Never use upper-case letters, or strings with an initial upper-case letter to name a variable in Mathematica as it can conflict with internal names. $\endgroup$ – David G. Stork Dec 17 '17 at 20:08
7
$\begingroup$
result = J;
result[[All, All, 3]] += L[[All, All, 3]]
$\endgroup$
5
$\begingroup$

Something like:

MapThread[{#1[[1]], #1[[2]], #1[[3]] + #2[[3]]} &, {J, L}, 2]

do the job.

$\endgroup$
5
$\begingroup$
tJ = Table[{x10, y10, x10*y10}, {x10, 0, 1, 0.5}, {y10, 0, 1, 0.5}];
tL = Table[{x10, y10, 2.0*x10*y10}, {x10, 0, 1, 0.5}, {y10, 0, 1, 0.5}];

Several alternatives:

tK1 = Map[{0, 0, 1} # &, tJ, {-2}] + tL;
tK2 = MapAt[2 # &, Mean[{tJ, tL}], {{All, All, -1}}];
tK3 = Mean[{tJ, tL}]; tK2[[All, All, -1]] = 2 tK2[[All, All, -1]];

Grid[tK1, Dividers -> All] // TeXForm

$\begin{array}{|c|c|c|} \hline \{0.,0.,0.\} & \{0.,0.5,0.\} & \{0.,1.,0.\} \\ \hline \{0.5,0.,0.\} & \{0.5,0.5,0.75\} & \{0.5,1.,1.5\} \\ \hline \{1.,0.,0.\} & \{1.,0.5,1.5\} & \{1.,1.,3.\} \\ \hline \end{array}$

tK1 == tK2 == tK3

True

$\endgroup$
4
$\begingroup$

Another possibility

t3 = Map[(# /. {a_, b_, c_} -> {0, 0, c}) &, t1, {2}] + t2
$\endgroup$
2
$\begingroup$
j = Table[{x10, y10, x10*y10}, {x10, 0, 1, 0.5}, {y10, 0, 1, 0.5}];
l = Table[{x10, y10, 2.0*x10*y10}, {x10, 0, 1, 0.5}, {y10, 0, 1, 0.5}];

Partition[
 Transpose[{Partition[Flatten[j], 3], Partition[Flatten[l], 3]}] /. 
  {{x1_, y1_, r1_}, {x2_, y2_, r2_}} :> {x1, y1, r1 + r2}
, 3]

{{{0., 0., 0.}, {0., 0.5, 0.}, {0., 1., 0.}}, 
 {{0.5, 0., 0.}, {0.5, 0.5, 0.75}, {0.5, 1., 1.5}}, 
 {{1., 0., 0.}, {1., 0.5, 1.5}, {1., 1., 3.}}}
$\endgroup$

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.