Automatically create array of named variables

In summary, you can use ToExpression and StringRiffle to create a list of locators that can be used with ListPlot.
  • #1
Swamp Thing
Insights Author
908
572
In the following example...
Code:
Manipulate[
             ListPlot[{p1, p2}, PlotRange -> {{0, 3}, {0, 3}}, Joined -> True],
             {{p1, {0.5, 0.5}}, Locator},
            {{p2, {1, 0.7}},   Locator}
         ]

... we have created variables p1 and p2 that are local to the Manipulate block, and we use them to set up two locators on the ListPlot. Now if we want to create a large number of locators, we could manually type in a large number of p1, p2, p3, p4 ... and so on. But is there a way to build these arrays of named variables programmatically?

The above is just one example, but there are other constructs in Mathematica where an input to a function must contain multiple named variables. It would be nice to automate the creation of large lists like that.
 
Physics news on Phys.org
  • #2
A bit of googling turned up the concept of indexed variables. For example, we can do :
P = Table[Indexed[p, i], {i, 1, 7}]

which gives us ##P = \{ p_1, p_2, p_3, p_4, p_5, p_6, p_7 \}## , and we can use these as our seven variables.

This takes us one step forwards... But unfortunately, when we look at the locator specification at the end, it doesn't have a true list like format. It's just a series of extra arguments to the Manipulate. So the question turns into, "how can we feed in multiple arguments into a function automatically, when it can accept a series of arguments?"========== Edit: I realized that the indexed variables don't work as I thought ===
 
  • #3
  • #4
I'll look them up and give them a try... thanks.
 
  • #5
ListPlot will iterate over a list of expressions:

Code:
P = Table[Indexed[p, i], {i, 1, 7}]
pointList = Table[{RandomReal[{0, 3}], RandomReal[{0, 3}]}, {i, 1, 7}]
Manipulate[
ListPlot[P, PlotRange -> {{0, 3}, {0, 3}},
  Joined -> True], {{P, pointList}, Locator}]
 
  • Love
Likes Swamp Thing
  • #6
cabrera said:
ListPlot will iterate over a list of expressions:

Really neat. Thanks!

I had tried unsuccessfully to get Manipulate to accept a list of locators, but I gave up as I couldn't figure out the right syntax... and I wrongly concluded that it just isn't designed to take a bracketed list.

Even now, your code has some subtlety that I can barely wrap my head around.
 

What is an array?

An array is a data structure that allows you to store a collection of values in a single variable. Each value in an array is assigned a unique index, which allows for easy retrieval and manipulation of the data.

How do you automatically create an array of named variables?

An array of named variables can be automatically created by using a loop to assign values to each index in the array. This can be done using a simple for loop or a more advanced method such as the array_map function in some programming languages.

What are the benefits of using an array of named variables?

Using an array of named variables can make your code more organized and efficient. It allows you to store and manipulate multiple values using a single variable, which can save time and reduce the amount of code needed. Arrays also allow for faster retrieval of data compared to other data structures.

Can an array of named variables hold different types of data?

Yes, an array of named variables can hold different types of data. This means that you can store integers, strings, booleans, and other types of data in the same array. This flexibility makes arrays a useful tool in many programming tasks.

How can you access and manipulate data in an array of named variables?

To access and manipulate data in an array of named variables, you can use the index of the value you want to retrieve or modify. You can also use various methods and functions that are specific to arrays, such as sorting, filtering, and adding or removing elements.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
719
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Quantum Physics
Replies
1
Views
890
  • Programming and Computer Science
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
13K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
6K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
Back
Top