Reputation: 1511
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
Upvotes: 4
Views: 22460
Reputation: 249153
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.
Upvotes: 12