1
$\begingroup$

From a vector q={Gamma,x,y}, I would like to create the following corresponding list : qn={1,2,3}.

Other example, if I have q={Gamma,x,y,theta}, I would like to retrieve qn={1,2,3,4}.

Thank you for your help.

Is there a better way than Table[i, {i, 1, Length[q]}]?

Thank you

$\endgroup$
  • 4
    $\begingroup$ Range[Length[q]] $\endgroup$ – Jason B. Nov 12 '15 at 10:26
  • $\begingroup$ @JasonB probably you should post it as an answer. $\endgroup$ – rhermans Nov 12 '15 at 10:38
  • $\begingroup$ Right! For me, it is perfect. Thank you $\endgroup$ – Bendesarts Nov 12 '15 at 10:39
  • 1
    $\begingroup$ What would you like to get for {Gamma, x, x, Gamma}? $\endgroup$ – Kuba Nov 12 '15 at 10:44
4
$\begingroup$

This will give what you are looking for

q = {Gamma, x, y, theta};
Range[Length[q]]
(* {1, 2, 3, 4} *)

Or, with less typing,

Range@Length@q
(* {1, 2, 3, 4} *)
$\endgroup$
2
$\begingroup$

As @Jason already commented, the usual form would be

Range @ Length @ q;

If your goal is to number the list you can do

> Transpose[{Range @ Length @ q, q}]

{{1, Gamma}, {2, x}, {3, y}, {4, theta}}

or use MapIndexed

MapIndexed[List, q]

{{Gamma, {1}}, {x, {2}}, {y, {3}}, {theta, {4}}}

$\endgroup$
  • $\begingroup$ Or, do MapIndexed[Reverse@*Flatten@*List, q] to get it in the normal form. $\endgroup$ – march Nov 12 '15 at 17:17

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.