3
$\begingroup$

From an excel file, I have the following form of the (raw) data:

{{x1, y11, y12, y13, y14}, {x2, y21, y22, y23, y24}, ..., {xn, yn1, yn2, yn3, yn4}}

Here, all innermost elements are all real numbers. I want to make a ListPlot from points {xi, yij} for each i and j. The points with same j are to be joined by a line segment. Then, I should be able to manipulate the above list, for example as

{{{x1, y11}, {x2, y21}, ..., {xn, yn1}}, ..., {{x1, y14}, {x2, y24}, ..., {xn, yn4}}}

How can I do this?

$\endgroup$
4
$\begingroup$
data = {{x1, y11, y12, y13, y14},
        {x2, y21, y22, y23, y24},
        {x3, y31, y32, y33, y34}};

Table[{e[[1]], e[[j + 1]]}, {j, Length@First@data - 1}, {e, data}]

(** {{{x1, y11}, {x2, y21}, {x3, y31}},
     {{x1, y12}, {x2, y22}, {x3, y32}},
     {{x1, y13}, {x2, y23}, {x3, y33}},
     {{x1, y14}, {x2, y24}, {x3, y34}}} **)
$\endgroup$
1
  • $\begingroup$ Thanks for such a simple answer! $\endgroup$
    – eigenvalue
    Jul 30 at 12:14
3
$\begingroup$
Clear["Global`*"]

Format[x[n_]] := Subscript[x, n]
Format[y[m_, n_]] := Subscript[y, Row[{m, n}]]

(data = {{x[1], y[1, 1], y[1, 2], y[1, 3], y[1, 4]},
    {x[2], y[2, 1], y[2, 2], y[2, 3], y[2, 4]},
    {x[3], y[3, 1], y[3, 2], y[3, 3], y[3, 4]},
    {x[4], y[4, 1], y[4, 2], y[4, 3], y[4, 4]},
    {x[5], y[5, 1], y[5, 2], y[5, 3], y[5, 4]}}) // MatrixForm

enter image description here

Use a helper function to form the points

convertList[lst_?VectorQ] :=
 {First@lst, #} & /@ (Rest@lst)

(data2 = Transpose[convertList /@ data]) // MatrixForm

enter image description here

When data is defined with numeric values use

ListLinePlot[data2]
$\endgroup$
3
$\begingroup$
Thread[{First@#,Rest@#}]&/@data//Transpose

(* {
   {{x1, y11}, {x2, y21}, {x3, y31}}, 
   {{x1, y12}, {x2, y22}, {x3, y32}}, 
   {{x1, y13}, {x2, y23}, {x3, y33}}, 
   {{x1, y14}, {x2, y24}, {x3, y34}}
   } *)

Original Answer:

Distribute[{First@#, Rest@#},List]&/@data//Transpose

Alternative form:

 ArrayReduce[Thread[{First@#,Rest@#}]&, data, 2]//Transpose

where

data = {{x1, y11, y12, y13, y14},
        {x2, y21, y22, y23, y24},
        {x3, y31, y32, y33, y34}};
$\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.