6
$\begingroup$

This question already has an answer here:

I have a list of real numbers, say:

  vec = RandomInteger[{1, 20}, 6]
(* {2, 4, 7, 10, 13, 6} *)

I want to convert each of these numbers to the corresponding point on the x-axis, that is, to obtain:

  {{2, 0}, {4, 0}, {7, 0}, {10, 0}, {13, 0}, {6, 0}}

The purpose of doing this is to include these points, among other graphics objects, in the plot of a function.

Here are some ways to do it:

  Partition[Riffle[vec, 0], 2]
  (Append[#, 0] &) /@ Partition[vec, 1]
  PadRight[#, 2] & /@ Partition[vec, 1]
  vec /. x_?NumericQ -> {x, 0}

What is an especially simple way to do this that will be readily understandable by somebody relatively new to Mathematica -- especially somebody who may still be uncomfortable with employing pure functions, functional approaches, and pattern-matching?

$\endgroup$

marked as duplicate by Mr.Wizard Jun 21 '15 at 10:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Related: (7996), (45980) $\endgroup$ – Mr.Wizard Jun 21 '15 at 10:40
  • $\begingroup$ I see two reopen votes on this question yet the Accepted answer shows exactly the same methods as the answers in the original. The question itself is also identical except for an updated emphasis on a "simple way" but since the "simple way" is already shown in the original this does not differentiate it. $\endgroup$ – Mr.Wizard Jun 21 '15 at 21:09
11
$\begingroup$
vec = {2, 4, 7, 10, 13, 6};

{#, 0} & /@ vec

{{2, 0}, {4, 0}, {7, 0}, {10, 0}, {13, 0}, {6, 0}}

Thread[{vec, 0}]

{{2, 0}, {4, 0}, {7, 0}, {10, 0}, {13, 0}, {6, 0}}

$\endgroup$
  • $\begingroup$ I especially like Thread[{vec,0}] because it's not only short, but also avoids use of pure functions. $\endgroup$ – murray Jun 20 '15 at 15:14
  • $\begingroup$ Very much out-of-the-box approach with Thread +1 $\endgroup$ – LLlAMnYP Jun 20 '15 at 16:14
  • $\begingroup$ +1. Thread certainly is a beautiful solution. Nevertheless the performance advantage of a pure function seems hard to beat? $\endgroup$ – gwr Jun 20 '15 at 17:34
  • $\begingroup$ @gwr I doubt that Thread will be slower. As an in-built function, it should be quite efficient (but you are very welcome to benchmark). $\endgroup$ – Yves Klett Jun 20 '15 at 18:57
  • 1
    $\begingroup$ I did with 1 Mio random numbers in the vector and the pure function was faster by around 25 % though in absolute duration that did maybe not matter that much (around .13 secs for my machine for the Thread-sokution). $\endgroup$ – gwr Jun 20 '15 at 21:27
13
$\begingroup$

There's no need to convert points on the real line to points in the Cartesian plane.

Simply use NumberLinePlot with Spacings-> 0

NumberLinePlot[{2, 4, 7, 10, 13, 6}, Spacings -> 0]

points

$\endgroup$
  • 1
    $\begingroup$ I'm afraid using NumberLinePlot doesn't solve the problem, as the points on the x-axis I want to form are going to go into the Epilog of a Plot expression. And it's not obvious how to combine a NumberLinePlot with a Plot by means of Show, even though both have head Graphics. $\endgroup$ – murray Jun 21 '15 at 15:35
  • $\begingroup$ You may easily combine Plot and NumberLinePlot. For example, Show[{ Plot[2 Sin[x] + x, {x, 0, 15}], NumberLinePlot[{2, 4, 7, 10, 13, 6}, Spacings -> 0]}] $\endgroup$ – DavidC Jun 21 '15 at 15:41
  • 1
    $\begingroup$ you're right, I'm wrong. My test with Show had a typo! $\endgroup$ – murray Jun 21 '15 at 16:25
6
$\begingroup$

How about

 SeedRandom[1]; {#, 0} & /@ RandomInteger[{1, 20}, 6]
{{6, 0}, {1, 0}, {8, 0}, {1, 0}, {3, 0}, {4, 0}}
$\endgroup$
  • $\begingroup$ Sorry I couldn't accept two answers: your use of {#,0}&/@ came first. But @Bob Hanlon's answer included also the so-nice use of Thread. $\endgroup$ – murray Jun 20 '15 at 16:49
5
$\begingroup$

If the starting point is vec one approach would be

Transpose[{vec, ConstantArray[0, Length[vec]]}]
$\endgroup$
  • $\begingroup$ Even though this approach is hardly the tersest, it is very easy to understand, even by a beginner: no pure functions, no use of Map, no other functional construct such as Thread. (Having grown up programming in APL, this way should have occurred to me.) $\endgroup$ – murray Jun 21 '15 at 15:38
5
$\begingroup$

There are of course more compact solutions but I like the readability of Function avoiding the slot notations:

f = Function[ x, {x, 0}, Listable ];

f @ { 1, 2, 3, 4, 5, 6}

{{1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}}

A thing to note may be that using g instead of f

g = {#, 0}& /@ #&

eventually is much faster and also beats many other solutions.

$\endgroup$
  • $\begingroup$ Actually, efficiency was not among my concerns; brevity and, especially, transparency to those relatively new to Mathematica were. (Perhaps I should have said that.) $\endgroup$ – murray Jun 20 '15 at 18:44
  • $\begingroup$ That is why I sometimes go for the named function as it is so close to mathematics. While beatifully compact I do find other solutions quite unfortunate with regard to transparency and ease of understanding. $\endgroup$ – gwr Jun 20 '15 at 21:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.