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?
Answer
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}}
Comments
Post a Comment