# How can I assign values to individual matrix locations?

Posted 9 years ago
5140 Views
|
4 Replies
|
1 Total Likes
|
 I want to assign a value to one location in an existing matrix, but I get an error.The desired result is {{1, 2, -3}, {3, -1, 6}}. Can someone please help?
4 Replies
Sort By:
Posted 9 years ago
 This is strange; the code above works perfectly on my machines (Win7 / MMA9) and (Linux / MMA10): In[1]:= m = {{1, 2, -3}, {0, -1, -6}} m[[2]][[1]] m[[2]][[1]] = 3 m Out[1]= {{1, 2, -3}, {0, -1, -6}} Out[2]= 0 Out[3]= 3 Out[4]= {{1, 2, -3}, {3, -1, -6}} @Daniel Lichtblau: Thank you for pointing out the principal difference in the notation [[n,m]] and [[n]][[m]]! Once one knows it, it becomes quite obvious ...
Posted 9 years ago
 I stand corrected. Apparently that did work as an lvalue. I'm thoroughly confused now. But corrected. (Better than being certain and wrong though.)
Posted 9 years ago
 You need to use the unnested form e.g. m[[2,1]]=3. As an rvalue (in the CS sense) m[[2]][[1]] is fine, but it does not work as an lvalue (that is, something to which one can assign a value) in Mathematica.
Posted 9 years ago
 Awesome! Thank you. I know this seems really basic, but stuff like this really slows things down.