4

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
1
  • 1
    What about doing M[:,0] = 0 Commented May 16, 2016 at 11:02

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

1 Comment

this answer doesn't actually answer the title of this question, only partially when combined with OPs post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.