2
$\begingroup$

Consider two tables:

tab1=Flatten[Table[{i,j,0},{i,0.5,0.7,0.1},{j,0.1,0.3,0.1}],{1,2}];
tab2=Flatten[Table[{i,j,i*j},{i,0.5,0.7,0.2},{j,0.1,0.3,0.2}],{1,2}];

These tables have a common subset of the first and second elements of their rows: e.g., {0.5,0.1}, and {0.7,0.3}. How to make a table from tab1 where zeros (the third element) in the rows from the common subset are replaced with the corresponding values from tab2?

$\endgroup$

2 Answers 2

4
$\begingroup$

Something along these lines might work:

tab2Rules = Thread[ReplacePart[3 -> _] /@ tab2 -> tab2];
tab1 /. tab2Rules

{{0.5, 0.1, 0.05}, {0.5, 0.2, 0}, {0.5, 0.3, 0.15}, {0.6, 0.1, 0}, {0.6, 0.2, 0}, {0.6, 0.3, 0}, {0.7, 0.1, 0.07}, {0.7, 0.2, 0}, {0.7, 0.3, 0.21}}

$\endgroup$
3
$\begingroup$
Clear["Global`*"]
tab1 = Flatten[
   Table[{i, j, 0}, {i, 0.5, 0.7, 0.1}, {j, 0.1, 0.3, 0.1}], {1, 2}];
tab2 = Flatten[
   Table[{i, j, i*j}, {i, 0.5, 0.7, 0.2}, {j, 0.1, 0.3, 0.2}], {1, 
    2}];

Create rules for a Lookup:

r1 = Sequence @@@ ({Most@# -> Last@#} & /@ tab2)

{{0.5, 0.1} -> 0.05, {0.5, 0.3} -> 0.15, {0.7, 0.1} -> 0.07, {0.7, 0.3} -> 0.21}

Sequence @@@ {Most@#, Lookup[r1, {Most@#}, Last@#]} & /@ tab1 

{{0.5, 0.1, 0.05}, {0.5, 0.2, 0}, {0.5, 0.3, 0.15}, {0.6, 0.1, 0}, {0.6, 0.2, 0}, {0.6, 0.3, 0}, {0.7, 0.1, 0.07}, {0.7, 0.2, 0}, {0.7, 0.3, 0.21}}

$\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.