First row of numpy.ones is still populated after referencing another matrix

前端 未结 5 954
说谎
说谎 2021-01-26 10:01

I have a matrix \'A\' whose values are shown below. After creating a matrix \'B\' of ones using numpy.ones and assigning the values from \'A\' to \'B\' by indexing \'i\' rows an

5条回答
  •  有刺的猬
    2021-01-26 11:02

    1. As stated above: python indices start at 0.

    2. In order to iterate over some (say matrix) indices, you should use the builtin function 'range' and not 'numpy.arange'. The arange returns an ndarray, while range returns a generator in a recent python version.

    3. The syntax 'B[i:j]' does not refer to the element at row i and column j in an array B. It rather means: all rows of B starting at row i and going up to (but not including) row j (if B has so many rows, otherwise it returns until includingly the last row). The element at position i, j is in fact 'B[i,j]'.

    The indexing syntax of python / numpy is quite powerful and performant.

提交回复
热议问题