1
$\begingroup$

This question already has an answer here:

Let's say I have several variables, named R1, R2 and R3.

I wanted to have a function function[numberOfR_] and I wanted to have the output of the function to be R1 if the entered numberOfR = 1, R2 if the entered numberOfR = 2, etc. I tried defining this function as follows

function[numberOfR_] = "R" + numberOfR_;

but it doesn't put the R and the number together as I hoped; instead, it just gives me

 3+R' or '2+R'

not as a single variable name like R3 or R2. What can I do to solve this?

$\endgroup$

marked as duplicate by Yves Klett, Sjoerd C. de Vries, dr.blochwave, m_goldberg, Kuba Jul 2 '15 at 8:22

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.

  • 4
    $\begingroup$ function[numberOfR_] := Symbol /@ (("r" <> ToString@#) & /@ Range@numberOfR) to give a list of a range, function[numberOfR_] := Symbol@("r" <> ToString@numberOfR) for one-shot - and don't use uppercase initials for your symbols - asking for trouble... $\endgroup$ – ciao Jul 2 '15 at 5:20
1
$\begingroup$

This is your function, but this kind of programming seems to be weird, try other ways to do want you want

f[numberOfR_] := Symbol["R" <> ToString[numberOfR]]
$\endgroup$

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