5

I have a 3x3 numpy array and I want to divide each column of this with a vector 3x1. I know how to divide each row by elements of the vector, but am unable to find a solution to divide each column.

3 Answers 3

9

You can transpose your array to divide on each column

(arr_3x3.T/arr_3x1).T
Sign up to request clarification or add additional context in comments.

Comments

2

Let's try several things:

In [347]: A=np.arange(9.).reshape(3,3)
In [348]: A
Out[348]: 
array([[ 0.,  1.,  2.],
       [ 3.,  4.,  5.],
       [ 6.,  7.,  8.]])

In [349]: x=10**np.arange(3).reshape(3,1)

In [350]: A/x
Out[350]: 
array([[ 0.  ,  1.  ,  2.  ],
       [ 0.3 ,  0.4 ,  0.5 ],
       [ 0.06,  0.07,  0.08]])

So this has divided each row by a different value

In [351]: A/x.T
Out[351]: 
array([[ 0.  ,  0.1 ,  0.02],
       [ 3.  ,  0.4 ,  0.05],
       [ 6.  ,  0.7 ,  0.08]])

And this has divided each column by a different value

(3,3) divided by (3,1) => replicates x across columns.

With the transpose (1,3) array is replicated across rows.

It's important that x be 2d when using .T (transpose). A (3,) array transposes to a (3,) array - that is, no change.

Comments

0

The simplest seems to be

A = np.arange(1,10).reshape(3,3)
b=np.arange(1,4)
A/b

A will be

array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

and b will be

array([1, 2, 3])

and the division will produce

array([[1. , 1. , 1. ],
       [4. , 2.5, 2. ],
       [7. , 4. , 3. ]])

The first column is divided by 1, the second column by 2, and the third by 3. If I've misinterpreted your columns for rows, simply transform with .T - as C_Z_ answered above.

Comments

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.