1
$\begingroup$

Consider the following two grids:

grid1 = RandomReal[{0, 1}, {3, 2}];
grid2 = Flatten[Thread[{#[[1]], #[[2]], Range[3]}] & /@ grid1, 1];

Therefore, grid2 is grid1 but with additional coordinate.

Next, consider the tables of values of two functions func1, func2 on grid1, grid2:

func1[x_,y_]=Exp[-x]*Cos[y];
func2[x_,y_,z_]=Exp[-x^2]Sin[y]/z;
vals1 = Join[grid1, {func1[#[[1]],#[[2]]]} & /@ grid1, 2];
vals2 = Join[grid2, {func2[#[[1]],#[[2]],#[[3]]]} & /@ grid2, 
   2];

I want to obtain the table

table=Join[grid2,{func1[#[[1]],#[[2]]]*func2[#[[1]],#[[2]],#[[3]]]}&/@grid2,2]

assuming that I do not know func1, func2, i.e., having pre-computed vals1, vals2.

Could you please tell me how to do this quickly (in reality, vals1, vals2 are large multi-dimensional datasets)?

$\endgroup$
1
  • 3
    $\begingroup$ Join[grid2, List /@ Flatten[vals1[[All, -1]] Partition[vals2[[All, -1]], 3]], 2]? $\endgroup$
    – kglr
    Apr 16 at 21:14

1 Answer 1

1
$\begingroup$
Join[grid2, 
 List /@ Flatten[vals1[[All, -1]] Partition[vals2[[All, -1]], 3]], 2]

enter image description here

% == table

True

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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