Group Abstract Group Abstract

Message Boards Message Boards

0
|
4.6K 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
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

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
POSTED BY: Nikolay Shilov
POSTED BY: Joonas Hirvonen
Posted 6 years ago
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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard