# Weird behavior when changing parts of matrix

I have this matrix:

mat = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16,
17, 18, 19, 20}, {21, 22, 23, 24, 25}};


and here are its second row and second column:

{mat[[2]], mat[[All, 2]]}
(* {{6, 7, 8, 9, 10}, {2, 7, 12, 17, 22}} *)


When I tried to change its second column to all zeros, this method works:

mat = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16,
17, 18, 19, 20}, {21, 22, 23, 24, 25}};
mat[[All, 2]] = 0;
mat // MatrixForm


But using the method to change the second row, the entire row become a big zero:

mat = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16,
17, 18, 19, 20}, {21, 22, 23, 24, 25}};
mat[[2]] = 0;
mat // MatrixForm


My question is:

1) Why would the replacement of a row versus a column in a matrix different?

2) Why would setting the list of all column elements to zero automatically set each of them to zero? Setting a list to a single number doesn't seem to work in isolation:

{a, b, c} = 0


• I think this behavior is correct, because what if I wanted the second element of the list mat to be 0? This is the logical way to do that. Aug 3, 2014 at 5:35
• I think it's correct too. The part that I found confusing is more on the replacement of the column, which by this method seems to be an element-by-element replacement (instead of replacing a whole row to a single 0 in comparison). If so, I can't see how setting a list of all these column elements would set them all to zero instead of, say, giving me an error. Also, doing this doesn't seem to work:mat = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}, {21, 22, 23, 24, 25}}; {mat[[1, 2]], mat[[2, 2]], mat[[3, 2]], mat[[4, 2]], mat[[5, 2]]} = 0; mat. Aug 3, 2014 at 5:48

Actually, I think the reason it works like this is because Mathematica lists can be anything, so it assumed you want the second element of this list of lists to be 0. So if you want all elements of that second sublist to be 0, you have to specify it just like you did for the column.

mat = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16,
17, 18, 19, 20}, {21, 22, 23, 24, 25}};


Then:

mat[[2, All]] = 0


OR

mat[[2, ;;]] = 0

mat // MatrixForm


• Hm... you changed your answer and now mine is rather a duplicate. Nevertheless I stated things a bit differently so I'm going to leave it, if you don't mind. Aug 3, 2014 at 5:45
• @Mr.Wizard, it's fine, I don't mind. Aug 3, 2014 at 5:47
• By the way I want to let you know that I have noticed the rapid "reputation" gain you've made over the last few weeks – impressive! Aug 4, 2014 at 0:11
• @Mr.Wizard, thanks. It means a lot coming from you. It's probably going to slow down this week, I'm about to start writing some papers :) Aug 4, 2014 at 0:17
• @Mr.Wizard - As my prime moderator ("whig") you should refrain from such comments. It's like a teacher saying: "Buddy, I notice you, but I don't don't notice all the others in my class." If I may quote yourself: "As a Community Moderator my conduct should be held to a higher standard (i.e. if I'm doing it wrong people should criticize me)". I hope you don't mind my criticism. :)
– eldo
Aug 4, 2014 at 21:05

This is simply the logical outcome of Mathematica treating an array as an ordinary expression tree.

Your array isn't really an array at all, but merely a collection of List expressions inside another List expression, all of which happen to be the same length. And like any other ordinary expression in an assignment you can change a part using Set:

expr = foo[bar, baz];

expr[[2]] = 0;

expr

foo[bar, 0]


While Mathematica can store a true array in the case of packed arrays these are handled in such a way as to make operations behave identically to the same operation on the equivalent ordinary expression. (There are exception but I digress.)

If you wish to zero all values within a row you can specially address all parts of the row as RunnyKine did, with e.g. a[[2, All]] = 0;, or you can simply multiply the row by 0, since Times is a Listable operation:

a = Partition[Range@9, 3];

a[[2]] *= 0;

a

{{1, 2, 3}, {0, 0, 0}, {7, 8, 9}}