1
$\begingroup$

I have some data which are structured poorly, and I want to interpolate them. This is the way they are structured:

{{z1_1,z1_2,....z1_21}, {z2_1,z2_2,....z2_21}, ....{z11_1,z_11_2, ....z11_21}}

And then I have separate arrays which contain the x and y data. {x1,x2, ....x11} And {y1,y2, ....y21}

If I understand the interpolation function correctly, then the data for interpolation should be structured like this:

{{{x1,y1},z1_1},{{x1,y2},z1_2}, ....{{x11,y21},z11_21}}

Unfortunately, I have no clue how to re-structure the data.

$\endgroup$

3 Answers 3

1
$\begingroup$

I'll set the bar very low to start this

x={x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11};
y={y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12,y13,y14,y15,y16,y17,y18,y19,y20,y21};
z=Table[21*(i-1)+j,{i,11},{j,21}];
result={};
For[xi=1,xi<=11,xi++,
 For[yi=1,yi<=21,yi++,
  AppendTo[result,{x[[xi]],y[[yi]],z[[xi,yi]]}]
  ]];
result

Now we just need 8 more increasingly impressive, increasingly unimaginable completely different ways of doing this

$\endgroup$
1
$\begingroup$

As an example:

xlist = "x" <> ToString@# & /@ Range[11];
ylist = "y" <> ToString@# & /@ Range[21];
zlist = Table["z" <> ToString@i <> ToString@j, {i, 1, 11}, {j, 1, 21}];

data = Thread[{Outer[List, xlist, ylist], zlist}] // Map[Transpose]

You can Flatten it if required. Add // Flatten[#, 1] & at the end.

$\endgroup$
1
$\begingroup$
MapThread[List/*Transpose/*Splice, {Outer[List, xs, ys], zs}]

or

Flatten[MapThread[List, {Outer[List, xs, ys], zs}, 2], 1]

Where xs is your list of 11 x coords, ys is your list of 21 y coords, and zs is your 11 by 21 array of z values.

Riffing on Bill's solution:

Flatten[
  Table[
    {{xs[[xidx]], ys[[yidx]]}, zs[[xidx, yidx]]}, 
    {xidx, xCount}, 
    {yidx, yCount}], 
  1]

Where I've abstracted out the dimensions. In your specific case xCount would be 11 and yCount would be 21.

$\endgroup$

Your Answer

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

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