Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

Mathematica graphics

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

Mathematica graphics


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

Mathematica graphics

share|improve this question
    
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. –  Chip Hurst Aug 3 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. –  seismatica Aug 3 at 5:48

2 Answers 2

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

Mathematica graphics

share|improve this answer
    
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. –  Mr.Wizard Aug 3 at 5:45
    
@Mr.Wizard, it's fine, I don't mind. –  RunnyKine Aug 3 at 5:47
    
Okay. :-) ..... –  Mr.Wizard Aug 3 at 5:48
    
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! –  Mr.Wizard Aug 4 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 :) –  RunnyKine Aug 4 at 0:17

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}}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.