5

I have a list of numbers corresponding to the known ordinates of some points:

x = {60, 101, 61, 203, 248, 346, 464,
     635, 635, 958, 1019, 1020, 1182, 
     1276, 1397, 1607, 1710, 1869, 1975}

How can I generate a new list of points based on that one by adding an index, such as

P = {{1, 60}, {2, 101}, {3, 61}, ....}
MarcoB
  • 67,153
  • 18
  • 91
  • 189
Mertin
  • 177
  • 5

4 Answers4

7

This is most likely the most efficient approach:

Transpose[{Range[Length[x]], x}]
 {{1, 60}, {2, 101}, {3, 61}, {4, 203}, {5, 248}, {6, 346}, {7, 464}, {8, 635}, 
  {9, 635}, {10, 958}, {11, 1019}, {12, 1020}, {13, 1182}, {14, 1276}, {15, 1397}, 
  {16, 1607}, {17, 1710}, {18, 1869}, {19, 1975}}

Another approach (less efficient), that one could mention:

MapIndexed[ Flatten @ {#2, #1} &, x]

In a more general case instead of a strictly homogeneous distribution of the domain points we might deal with a random distribution which is still globally homogeneous, e.g.

y = RandomReal[{-1/2, 1/2}, #] + Range[#] & @ Length[x]
{ 1.01273, 1.6359, 3.38264, 4.08369, 5.05749, 6.12815, 7.35125, 7.93196, 
  8.97418, 10.2015, 10.824, 12.0907, 13.2303, 14.3775, 15.2711, 16.3996,
 17.3865, 17.7342, 19.4849}

and then we generate the list simply with:

Transpose[{y, x}]
Artes
  • 57,212
  • 12
  • 157
  • 245
5

If you really want to have an easy solution:

ResourceFunction["AddIndices"][x]
SHuisman
  • 3,258
  • 8
  • 12
1
P=Table[{k,x[[k]]},{k,1,Length[x]}]
ZeroTheHero
  • 401
  • 3
  • 11
1

For variety:

TimeSeries[x, {1}]["Path"]
{{1, 60}, {2, 101}, {3, 61}, {4, 203}, {5, 248}, {6, 346}, {7, 464}, 
 {8, 635}, {9, 635}, {10, 958}, {11, 1019}, {12, 1020}, {13, 1182}, 
 {14, 1276}, {15, 1397}, {16, 1607}, {17, 1710}, {18, 1869}, {19, 1975}}
TemporalData[x, {1}]["Path"]
{{1, 60}, {2, 101}, {3, 61}, {4, 203}, {5, 248}, {6, 346}, {7, 464}, 
 {8, 635}, {9, 635}, {10, 958}, {11, 1019}, {12, 1020}, {13, 1182}, 
 {14, 1276}, {15, 1397}, {16, 1607}, {17, 1710}, {18, 1869}, {19, 1975}}
kglr
  • 394,356
  • 18
  • 477
  • 896