Group Abstract Group Abstract

Message Boards Message Boards

0
|
4.2K Views
|
6 Replies
|
1 Total Like
View groups...
Share
Share this post:

Set elements of an array repeatedly without a loop?

Posted 6 years ago

Let's say that I have an array of variables v[i]:

v={vVar[1],vVar[2],vVar[3]}

I would like to set the variables to have some values.

I have multiple ways of setting their value for the first time. For example:

values={0,1,2};
Evaluate[v]=values;

None of the procedures I have found can do this repeatedly. If I try to do the following, it will complain that "Cannot assign to raw object".

values1={0,1,2};
Evaluate[v]=values1;
values1={3,4,5};
Evaluate[v]=values2;

And the variables will have the values from list values1.

There is obviously a possibility of defining every vVar[i] though a loop but I'm wondering if there is a more sophisticated method of doing this.

Edit: changing the variable names

POSTED BY: Joonas Hirvonen
6 Replies
Posted 6 years ago

Just do the assignments:

In[1]:= values = {vVar[1], vVar[2], vVar[3]}
Out[1]= {vVar[1], vVar[2], vVar[3]}

In[2]:= values = {0, 1, 2}
Out[2]= {0, 1, 2}

In[3]:= values = {3, 4, 5}
Out[3]= {3, 4, 5}

However, when you want retrive a single instance you need to specify it as a Part of values

In[4]:= values[[2]]
Out[4]= 4

vVar[2] will not work. Same thing goes if you want to modify a single instance:

In[5]:= values[[2]] = 12
Out[5]= 12

In[6]:= values
Out[6]= {3, 12, 5}
POSTED BY: Hans Milton
Posted 6 years ago

So, you have an expression involving v[i], {i, 1, n} that is the result of some lengthy computation, and you would like to evaluate that expression for different sets of v[i] values? If that is an accurate description of your problem then this is one way to do it.

Example expression

ClearAll[v];
y = v[1]*v[2] + v[3]^v[4]

Example sets of v[i] values to use in evaluating y

d1 = {1, 2, 3, 4};
d2 = {4, 3, 2, 1};

Use Replace to substitute values

y /. _[i_] :> d1[[i]]
(* 83 *)

y /. _[i_] :> d2[[i]]
(* 14 *)
POSTED BY: Rohit Namjoshi

Sorry for the naming mistake I made in the post. I used different names in my code and didn't see that mistake coming.

POSTED BY: Joonas Hirvonen

These variables that I have in this array are also in another variable. The goal would be to change the values of these variables so that I could easily re-evaluate the other variable.

(The other variable is a product of a quite lengthy initialization. Therefore I would prefer not to go through it every time but rather have it as a variable that can be re-evaluated.)

POSTED BY: Joonas Hirvonen
Posted 6 years ago

Hi Joonas,

Not sure exactly what you are trying to accomplish but maybe this satisfies your requirements

{v[1], v[2], v[3]} = {0, 1, 2};
v /@ {1, 2, 3}
(* {0, 1, 2} *)

{v[1], v[2], v[3]} = {3, 4, 5};
v /@ {1, 2, 3}
(* {3, 4, 5} *)
POSTED BY: Rohit Namjoshi

I have a question: what are you going to do with this array? Your current definition of v will lead to infinite recursion and execution of the second Code Sample won't help. So, could you please add some details?

POSTED BY: Nikolay Shilov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard