Set a column in numpy array to zero

14,860

You're doing this:

M[:][0] = 0

But you should be doing this:

M[:,0] = 0

The first one is wrong because M[:] just gives you the entire array, like M. Then [0] gives you the first row.

Similarly, M[0][:] gives you the first row as well, because again [:] has no effect.

Share:
14,860
S.AMEEN
Author by

S.AMEEN

Updated on August 03, 2022

Comments

  • S.AMEEN
    S.AMEEN almost 2 years

    I want to set a column in numpy array to zero at different times, in other words, I have numpy array M with size 5000x500. When I enter shape command the result is (5000,500), I think 5000 are rows and 500 are columns

    shape(M)
    (5000,500)
    

    But the problem when I want to access one column like first column

    Mcol=M[:][0]
    

    Then I check by shape again with new matrix Mcol

    shape(Mcol)
    (500,)
    

    I expected the results will be (5000,) as the first has 5000 rows. Even when changed the operation the result was the same

    shape(M)
    (5000,500)
    Mcol=M[0][:]
    shape(Mcol)
    (500,)
    

    Any help please in explaining what happens in my code and if the following operation is right to set one column to zero

    M[:][0]=0