0
$\begingroup$

I want to convert a list of data from

{{x11,y11,x21,y21,x31,y31,...xi1,yi1},{x12,y12,x22,y22,x32,y32,...xi2yi2},...{x1j,y1j,x2j,y2j,x3j,y3j,...xij,yij}}

to

{{z,x11,y11},{z,x12,y12},{z,x13,y13},...,{z,x1j,y1j},{z+10,x21,y21},{z+10,x22,y22},{z+10,x23,y23},...,{z+10,x2j,y2j},{z+20,x31,y31}...{z+10i,xij,yij}}

So this is actually a large table of data with xy values in each two successive columns. I want to insert z values in the first column and after each two columns and then convert it into a list of xyz values in order to plot it using ListPlot.
Here is a part of the table:

data={{290, 218.2, 310, 224.6, 335, 180.1}, {291, 138.4, 311, 161.2, 336, 
  129.5}, {292, 99.61, 312, 131.5, 337, 102.4}, {293, 94.15, 313, 
  129.8, 338, 95.39}, {294, 93.47, 314, 137.2, 339, 92.25}, {295, 
  95.82, 315, 144.2, 340, 89.9}, {296, 99.15, 316, 151.4, 341, 
  90.57}, {297, 104.9, 317, 161.9, 342, 91.05}, {298, 109.5, 318, 
  173.8, 343, 92.26}, {299, 114.5, 319, 188, 344, 95.52}};

Which I manually converted to the following by inserting z values of 280 for for first xy column, 295 for next xy column and 320 for the third xy column.

dataxyz={{280, 290, 218.2}, {280, 291, 138.4}, {280, 292, 99.61}, {280, 293, 
  94.15}, {280, 294, 93.47}, {280, 295, 95.82}, {280, 296, 
  99.15}, {280, 297, 104.9}, {280, 298, 109.5}, {280, 299, 
  114.5}, {295, 310, 224.6}, {295, 311, 161.2}, {295, 312, 
  131.5}, {295, 313, 129.8}, {295, 314, 137.2}, {295, 315, 
  144.2}, {295, 316, 151.4}, {295, 317, 161.9}, {295, 318, 
  173.8}, {295, 319, 188}, {320, 335, 180.1}, {320, 336, 129.5}, {320,
   337, 102.4}, {320, 338, 95.39}, {320, 339, 92.25}, {320, 340, 
  89.9}, {320, 341, 90.57}, {320, 342, 91.05}, {320, 343, 
  92.26}, {320, 344, 95.5}}

There should be some easy ways to do that, but I could not find it. Can anyone please help?
I was also thinking about extracting the data from the table using loops. Is it possible to pull the data from the table using something like that:

For[z=280,z<600,z=z+10,
 For[i=1,i<30,i=i+2,
  For[j=1,j<300,j++,
make a list of {z,x,y} from data[[j,i]]]]]

I'm not good with programming thought. Here is the small part of the plot I am trying to make: enter image description here
This whole thing can be done using spreadsheet, which I generally do, but it would be very nice if I can manipulate the list in mathematica itself. Thank you.

$\endgroup$
2
$\begingroup$
Table[
  MapIndexed[Prepend[ #[[2 i - 1 ;; 2 i]] , {280, 295, 320}[[i]]] &, 
   data ], {i, 1, 3}] // Join

I could not follow what pattern you want for z ( you are counting by 10 in the For loop example.. ? )

Here is a somewhat cleaner method:

Flatten[
  Partition[Riffle[#, {280, 295, 320}, {1, 7, 3}], 3] & /@ data,
  1]

note the output here is ordered differently, but for your plot that should not matter.

$\endgroup$
  • $\begingroup$ Thanks George. I used the Riffle function and worked like charm. Riffle[#, {280, 300, 320, 340, 360, 380, 400, 420}, {1, 22, 3}] & /@ data. Can you explain what does this &/@ thing do? When I put data directly in place of #, it inserted the numbers after two rows. $\endgroup$ – Uttam Pal May 16 '17 at 6:59

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.