Message Boards Message Boards

4 Replies
1 Total Likes
View groups...
Share this post:

How can I assign values to individual matrix locations?

Posted 10 years ago

I want to assign a value to one location in an existing matrix, but I get an error.

Mathematica image

The desired result is {{1, 2, -3}, {3, -1, 6}}. Can someone please help?

POSTED BY: Tommy Volk
4 Replies

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]] = 3

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 BY: Henrik Schachner

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 BY: Daniel Lichtblau

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 BY: Daniel Lichtblau
Posted 10 years ago

Awesome! Thank you. I know this seems really basic, but stuff like this really slows things down.

POSTED BY: Tommy Volk
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract