Sanket
Sanket

Reputation: 746

Dividing all but first element of a matrix

I want to divide all the elements of a column matrix but the first.

>>> import numpy as np
>>> t = np.matrix(np.ones((5,1)))
>>> t
matrix([[ 1.],
        [ 1.],
        [ 1.],
        [ 1.],
        [ 1.]])

My aim is obtain a new matrix (say n) which is of same dimensions as t with all but first element of t divided by a number (say 5)

>>> n
matrix([[1.],
        [0.2],
        [0.2],
        [0.2],
        [0.2]])

I'm new to numpy. Can someone help me on how this can be done?

Upvotes: 0

Views: 97

Answers (1)

behzad.nouri
behzad.nouri

Reputation: 77951

You can do:

>>> n = t.copy()
>>> n[1:] /= 5
>>> n
matrix([[ 1. ],
        [ 0.2],
        [ 0.2],
        [ 0.2],
        [ 0.2]])

alternatively,

>>> np.vstack([t[0], t[1:]/5])
matrix([[ 1. ],
        [ 0.2],
        [ 0.2],
        [ 0.2],
        [ 0.2]])

Upvotes: 3

Related Questions