2
$\begingroup$

I want to generate variables in the form of: r1 = number, r2 = number, r3 = number, etc

The 'numbers' are not necessarily the same and come form a previous calculation.

This is what I have:

z = 5;
v = ToExpression["r" <> ToString[#]] & /@ Range[z]
{r1, r2, r3, r4, r5}

What I want to get is: {r1=, r2=, r3=, r4=, r5=} but the variables are equal to calculated values.

I want flexibility in how many variables each calculation will take. I have been trying ideas I've found here on SE, but can't get further than this.

I can brute-force this and just enter every new variable each time, but if I end up with a large number of variables this won't work.

Anyone have a way to have this work?

$\endgroup$
10
  • 1
    $\begingroup$ I do not understand what you want. Symbol name can't have equal sign in it. I suggest you include a more complete description of what you are actually trying to achieve (eg. how do you calculate values amd how do you want to use them further). $\endgroup$
    – Domen
    Aug 8 at 21:03
  • $\begingroup$ Say I set some index, say n = 5 and I have a variable "r" that gets calculated prior, so I want to generate each "r" in order with the index and what it's equal to: r1=2, r2=3, r3=4, r5=5 etc $\endgroup$
    – fiz
    Aug 8 at 21:07
  • $\begingroup$ You have z, v, r1, r2 ... in your code. Where did n come from now? Just post the actual code you are using. $\endgroup$
    – Domen
    Aug 8 at 21:09
  • 2
    $\begingroup$ Looks like an XY problem to me. $\endgroup$
    – Domen
    Aug 8 at 21:14
  • 1
    $\begingroup$ @fiz I am a little confused why would want to make a bunch of variables with different names and set them equal to elements in a list, especially if it was a large list. Why not just index on the list whenever you need a certain value from it? $\endgroup$
    – ydd
    Aug 8 at 21:30

1 Answer 1

5
$\begingroup$

If I understand correctly, you have several options. Let's use some dummy data as your previously calculated data.

previouslyCalculated = Range[5, 10]

Option 1: Assign the list itself to a symbol, and do your indexing with Part:

r = previouslyCalculated;
r[[2]]
(* 6 *)

Option 2: Create down-values for a symbol:

MapIndexed[(s[#2[[1]]] = #1) &, previouslyCalculated]
s[2]
(* 6 *)

Option 3: Generate symbols programmatically and assign them:

Evaluate[Symbol["t" <> ToString[#]] & /@ Range[Length@previouslyCalculated]] = previouslyCalculated
t2
(* 6 *)

The actual mechanism (MapIndexed versus list assignment) for the latter two could be swapped, or some other mechanism could be used, but the point is to either generate symbol names on the fly or use down-values.

You seem to be wanting something along the lines of option 3, but I think that it's usually the worst of the three options. Option 1 is the best option in most circumstances, and there are cases where option 2 is useful.

Update

Based on one of your comments, it appears that what you are asking for is actually just a display, i.e. you don't actually want to set variables to the values. If that's the case, then something like this might work for you:

MapThread[
  StringForm["q`` = ``", ##] &,
  {Range[Length@previouslyCalculated], previouslyCalculated}]

Update 2

Based on your comment that "the variable names aren't important", maybe all you need is the indices. Maybe something like TableForm would work:

TableForm[
  List /@ previouslyCalculated, 
  TableHeadings -> {Range[Length@previouslyCalculated], None}]
$\endgroup$
2
  • $\begingroup$ Thanks lericr: I'm not sure I understand this, but I will review it and work with it and see if I'm able to have it work as I'm hoping. Thank you for the ideas and the help. $\endgroup$
    – fiz
    Aug 8 at 21:43
  • $\begingroup$ lericr! Yes, that's it! Your first update. That's what I've been trying to do. I saw the MapThread and StringForm in other related posts on SE but I wasn't able to adapt them and get them to work. I've tried various things and could get close but nothing was working. This is great, I appreciate the help! $\endgroup$
    – fiz
    Aug 8 at 21:58

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