2
$\begingroup$

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.

$\endgroup$

2 Answers 2

4
$\begingroup$

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.

$\endgroup$
9
  • $\begingroup$ Can i use this Riffle with Table? $\endgroup$ Commented Nov 25, 2020 at 12:33
  • $\begingroup$ Lets say i have , Table[{x, f[x]}, {x, 0, 12000, 595}] $\endgroup$ Commented Nov 25, 2020 at 12:34
  • 1
    $\begingroup$ Yes. {x1, f[x1]}, {x1, f[x1]}, {x2, f[x2]}, {x2, f[x2]}, ... until the end without repeating the last item, this was the aim. $\endgroup$ Commented Nov 25, 2020 at 13:04
  • 1
    $\begingroup$ using the third argument of Riffle (Riffle[ActCoorX, ActCoorX, {1, -3, 2}]) seems faster for large lists. (+1) $\endgroup$ Commented Nov 26, 2020 at 4:44
  • 1
    $\begingroup$ @user75507 - Riffle[list, x, {imin, imax, n}] creates a new list in which x appears if possible at positions imin, imin + n, imin + 2 n, ... , imax. Try va = Table[Subscript[a, i], {i, Range[5]}]; Riffle[va, x, {1, -3, 2}]. Riffle inserts x starting at position 1 and stops 3 positions from the end (the negative number says: count from the end of the list). When x is a list, Riffle uses items from the second list in place of x. With two identical lists, the result is the same as using Most@Riffle[list, list]. $\endgroup$ Commented Nov 28, 2020 at 3:06
5
$\begingroup$

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}}

$\endgroup$
1
  • $\begingroup$ thank you @cvgmt . Your syntax helps me to explore more about mathematica. Great! $\endgroup$ Commented Nov 25, 2020 at 12:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.