5
$\begingroup$

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}, ....}
$\endgroup$
  • 3
    $\begingroup$ {Range[Length@x], x}\[Transpose] $\endgroup$ – Rasoul-Ghadimi Jun 20 at 10:15
7
$\begingroup$

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}]
| improve this answer | |
$\endgroup$
  • $\begingroup$ How to load the abscissa from another list? $\endgroup$ – Mertin Jun 20 at 10:30
  • $\begingroup$ There is no abscissa, you have to generate it with Range, yielding {1,2, ..., 19} $\endgroup$ – Artes Jun 20 at 10:32
  • $\begingroup$ @Mertin You can get it with MapIndexed as well as I demonstrated in the post. $\endgroup$ – Artes Jun 20 at 10:38
  • 1
    $\begingroup$ Also, Thread[{Range[Length[x]], x}] $\endgroup$ – Bob Hanlon Jun 20 at 15:02
  • $\begingroup$ @BobHanlon, Good point, thanks! Although Transpose should be much faster than Thread, see e.g. Interlacing a single number into a long list where it is compared with Map and Tuples. $\endgroup$ – Artes Jun 20 at 19:41
5
$\begingroup$

If you really want to have an easy solution:

ResourceFunction["AddIndices"][x]
| improve this answer | |
$\endgroup$
1
$\begingroup$
P=Table[{k,x[[k]]},{k,1,Length[x]}]
| improve this answer | |
$\endgroup$
1
$\begingroup$

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}}
| improve this answer | |
$\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.