5
$\begingroup$

Suppose I have a list named DATA containing ten {x, y} elements. I need to prepend a column of data with 10 rows ranging from 0.1 to 1 with stepsize 0.1.

DATA = {{3, 4}, {5, 1}, {5, 2}, {1, 8}, {9, 5}, {7, 6}, {25, 15}, {1, 
   2}, {55, 0}, {10, 1}}
Table[Nest[Prepend[#, i] &, DATA[[j]], 1], {i, 0.1, 1, 0.1}, {j, 1, 10}]

However, the result is something bizarre. It is coming out to be a three-dimensional matrix.

enter image description here

How do I do it? It is due to some minor problem I guess, but I am unable to point it out.

$\endgroup$

5 Answers 5

8
$\begingroup$
data= {{3, 4}, {5, 1}, {5, 2}, {1, 8}, {9, 5}, {7, 6}, {25, 15}, {1, 
   2}, {55, 0}, {10, 1}}

Transpose@Join[{Range[0.1, 1, 0.1]}, Transpose[data]]

OR

MapThread[Join, {List /@ Range[0.1, 1, 0.1], data}]

result:

{{0.1, 3, 4}, {0.2, 5, 1}, {0.3, 5, 2}, {0.4, 1, 8}, {0.5, 9, 
  5}, {0.6, 7, 6}, {0.7, 25, 15}, {0.8, 1, 2}, {0.9, 55, 0}, {1., 10, 
  1}}
$\endgroup$
3
  • 1
    $\begingroup$ Thank you so much. You just helped me save weeks for calculation. $\endgroup$
    – user444
    Apr 7, 2022 at 11:27
  • 1
    $\begingroup$ I am glad I was able to save you weeks. Thanks for the accept. $\endgroup$
    – Syed
    Apr 7, 2022 at 11:27
  • 1
    $\begingroup$ or MapThread[Join[{#1},#2]&, {Range[0.1, 1, 0.1], data}] , nearly the same. $\endgroup$ Apr 7, 2022 at 14:03
7
$\begingroup$

I noticed that you want 0.1 ~ 1 to be prepended. (index/10)

So this also works.

MapIndexed[Prepend[#1,First[#2]/10.]&,data]

{{0.1, 3, 4}, {0.2, 5, 1}, {0.3, 5, 2}, {0.4, 1, 8}, {0.5, 9, 5}, {0.6, 7, 6}, {0.7, 25, 15}, {0.8, 1, 2}, {0.9, 55, 0}, {1., 10, 1}}

$\endgroup$
6
$\begingroup$

Using the level argument of Join

Join[List /@ Range[0.1, 1, 0.1], data, 2]

gives the expected result:

{{0.1, 3, 4}, {0.2, 5, 1}, {0.3, 5, 2}, {0.4, 1, 8}, {0.5, 9, 5}, 
 {0.6, 7, 6}, {0.7, 25, 15}, {0.8, 1, 2}, {0.9, 55, 0}, {1., 10, 1}}
$\endgroup$
3
$\begingroup$
data= {{3, 4}, {5, 1}, {5, 2}, {1, 8}, {9, 5}, {7, 6}, {25, 15}, 
       {1, 2}, {55, 0}, {10, 1}};
MapThread[Prepend,{data, Range[0.1, 1, 0.1]}]

    (*{
       {0.1,3,4},
       {0.2,5,1},
       {0.3,5,2},
       {0.4,1,8},
       {0.5,9,5},
       {0.6,7,6},
       {0.7,25,15},
       {0.8,1,2},
       {0.9,55,0},
       {1.,10,1}
      } 

In more general terms:

mat = Array[a, {3, 4}];
vect= {10,20,30};

(1) Insert vect into mat as column 2:

MapThread[Insert[##,2]&, {mat, vect}]


(* {
    {a[1,1],10,a[1,2],a[1,3],a[1,4]},
    {a[2,1],20,a[2,2],a[2,3],a[2,4]},
    {a[3,1],30,a[3,2],a[3,3],a[3,4]}
   } *)

vect may be a column from mat:

MapThread[Insert[#,#[[3]],1]&,{mat}]

(*{
   {a[1,3],a[1,1],a[1,2],a[1,3],a[1,4]}, 
   {a[2,3],a[2,1],a[2,2],a[2,3],a[2,4]}, 
   {a[3,3],a[3,1],a[3,2],a[3,3],a[3,4]}
  }*) 

(2) Append vect to mat as a column:

MapThread[Append, {mat, vect}]

(*{
    {a[1,1],a[1,2],a[1,3],a[1,4],10},
    {a[2,1],a[2,2],a[2,3],a[2,4],20},
    {a[3,1],a[3,2],a[3,3],a[3,4],30}
  }*) 

(3) Prepend vect to mat as a column:

MapThread[Prepend, {mat, vect}]

(*{
    {10,a[1,1],a[1,2],a[1,3],a[1,4]},
    {20,a[2,1],a[2,2],a[2,3],a[2,4]},
    {30,a[3,1],a[3,2],a[3,3],a[3,4]}
  }*)
$\endgroup$
3
$\begingroup$

Using Do and PrependTo:

data = {{3, 4}, {5, 1}, {5, 2}, {1, 8}, {9, 5},
        {7, 6}, {25, 15}, {1, 2}, {55, 0}, {10, 1}};

Module[{l = data}, Do[PrependTo[l[[i]], #[[i]]], {i, Length@#}] &@Range[0.1, 1, 0.1]; l]

(*{{0.1, 3, 4}, {0.2, 5, 1}, {0.3, 5, 2}, {0.4, 1, 8}, {0.5, 9, 5}, 
   {0.6, 7, 6}, {0.7, 25, 15}, {0.8, 1, 2}, {0.9, 55, 0}, {1., 10, 1}}*)

Or using Cases:

Cases[Thread@{List /@ Range[0.1, 1, 0.1], data}, {a_, b_} :> Catenate@{a, b}]

(*{{0.1, 3, 4}, {0.2, 5, 1}, {0.3, 5, 2}, {0.4, 1, 8}, {0.5, 9, 5}, 
   {0.6, 7, 6}, {0.7, 25, 15}, {0.8, 1, 2}, {0.9, 55, 0}, {1., 10, 1}}*)
$\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.