Last answered:

18 Jan 2021

Posted on:

23 Mar 2020

1

Adding a row and column vector in NumPy

As per the linear algebra rules we should not be bale to add a row and column vector . But i am able to do this operation in NumPy. Furthermore i am unable to understand the output.
Example:
v = np.array([1,2,3])
row_vector = v.reshape(1,3)
column_vector = v.reshape(3,1)

row_vector + column_vector
O/P

array([[2, 3, 4],
       [3, 4, 5],
       [4, 5, 6]])

Also i have a doubt that are row and column vectors , vectors or matrices. Because the row and column vectors have 2 dimesnions : (1,m) or (m,1) respectively.

Also using the attribute 'row_vector.ndim' gives output 2.

Any help will be appreciated.

Thanks.

 

2 answers ( 0 marked as helpful)
Instructor
Posted on:

24 Mar 2020

1
Hi Yash,  thank you for reaching out! The operation + sums the two vectors by adding corresponding elements.  For the example you've shown: Adding a 3x1 row_vector and 1x3 column_vector, results in a 3*3 matrix, where each element (i, j) equals: row_vector(j) + column_vector(i). So, for the first row in your example we'd have [ row_vector_1 + column_vector_1, row_vector_2 + column_vector_1, row_vector_3 + column_vector_1] = [1+1, 2+1, 3+1] Generally on the topic of vector sums using +:  To be able to use + the two elements need to be compatible. Two inputs have compatible sizes if, for every dimension, the dimension sizes of the inputs are either the same or one of them is 1.  Hope this sheds some light on the topic!   Best,  Eli

Submit an answer