0
|
4285 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
GROUPS:

# Where to find guidance on when/how lists/array/vector elements altered.

Posted 11 years ago
 Sometimes it seems I can change the value of an element in a list - sometimes I get a protected error message.I'm sure this is a common problem error.Is there a tutorial or general guide on this? Any dodges? Am I missing something fundamental?
3 Replies
Sort By:
Posted 11 years ago
 Thanks both - I'm wondering whether my problems were just being careless on the double brackets. Also, I had a learning curve on how to alter the value of parameters passed into a function {using Hold}.Possibly the two apects together got me confused.So ReplacePart is just a shorthand way to make multiple changes to a list?
Posted 11 years ago
 The ReplacePart function might be helpful as well here:http://reference.wolfram.com/mathematica/ref/ReplacePart.html
Posted 11 years ago
 It's difficult to tell what your problem might be without examples.This is one common mistake people make: In[1]:= list = {1, 2, 3}  Out[1]= {1, 2, 3}  In[2]:= list[2] = 7  During evaluation of In[2]:= Set::write: Tag List in {1,2,3}[2] is Protected. >>  Out[2]= 7In[3]:= list[[2]] = 7Out[3]= 7In[4]:= listOut[4]= {1, 7, 3}The difference here is that list[2] (without the double square brackets) is a function application. This evaluates to:{1,2,3}[2]=7or in FullForm:Set[List[1, 2, 3][2], 7]which causes the error (since List is a protected system symbol).On the other hand list[[2]] (double square brackets) is how to specify a part of an expression and = (Set) allows modifications on parts of lists.