S.AMEEN - 1 year ago 48

Python Question

I want to set a column in numpy array to zero at different times, in other words, I have numpy array

`M`

`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`

Answer Source

You're doing this:

```
M[:][0]
```

But you should be doing this:

```
M[:,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.