TransWikia.com

Repeat every $ x $ coordinate except the last one

Mathematica Asked by user75507 on March 28, 2021

Below is the code to print the x value.

 ActCoorX = Range[0, 12000, 500]

   {0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 
    4500, 5000, 5500, 6000, 6500, 7000, 7500, 8000, 8500, 9000, 9500, 
    10000, 10500, 11000, 11500, 12000}

Here I want to repeat every x coordinate. I tried with RepeatString and Subdivide, but then it repeats every x coordinate including last one which is not correct.

Also tried with Table,

Table[{x}, {x, 0, 12000, 1000}, {2}]

gives below which is again not not correct.

{{{0}, {0}}, {{1000}, {1000}}, {{2000}, {2000}}, {{3000}, {3000}}, 
{{4000}, {4000}}, {{5000}, {5000}}, {{6000}, {6000}}, {{7000}, 
{7000}}, {{8000}, {8000}}, {{9000}, {9000}}, {{10000}, {10000}}, 
{{11000}, {11000}}, {{12000}, {12000}}}

Are there any other ways to do repeat the coordinates? Thank you in advance.

2 Answers

A simple way to interleave a list with itself is Riffle. To remove the final value, use Most.

Most@Riffle[ActCoorX, ActCoorX]

Updates:

From your comment about Table[{x, f[x]}, {x, 0, 12000, 595}], I think you don’t need to repeat coordinates. Maybe this is what you need:

Table[{x, f[x]}, {x, ActCoorX}]

If you don’t want x and f[x] for the last item, then:

Table[{x, f[x]}, {x, Most@ActCoorX}]

Solution

From comments, the question is:

How to make a table of x and f[x] for repeated values of a list, but not repeating the last value.

The most direct way might be:

Table[{x, f[x]}, {x, Most@Riffle[ActCoorX, ActCoorX]}]

However, to avoid recomputing f[x], compute the table for {x, f[x]} one time, and then use Riffle and Most.

Most[Riffle[#,#]&@Table[{x, f[x]}, {x, ActCoorX}]]

The results are the same.

Correct answer by creidhne on March 28, 2021

I think you want

ActCoorX = Range[0, 12000, 500];
Partition[ActCoorX, 2, 1]

{{0, 500}, {500, 1000}, {1000, 1500}, {1500, 2000}, {2000, 2500}, {2500, 3000}, {3000, 3500}, {3500, 4000}, {4000, 4500}, {4500, 5000}, {5000, 5500}, {5500, 6000}, {6000, 6500}, {6500, 7000}, {7000, 7500}, {7500, 8000}, {8000, 8500}, {8500, 9000}, {9000, 9500}, {9500, 10000}, {10000, 10500}, {10500, 11000}, {11000, 11500}, {11500, 12000}}

Answered by cvgmt on March 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP