Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

In Python, Is there any way for differentiating between column and row vectors? Till now I'm using scipy, numpy, and let's say if I was about to give one a vector,

from numpy import *

Vector = array([1,2,3])

It can't able to say, whether I referred to a row or a column vector.

Moreover,

array([1,2,3]) == array([1,2,3]).transpose()

True

I also realized that functions on vectors from the described modules, don't need differentiation. Example, outer(a,b) or a.dot(b), but I want to differentiate them. Anyone give me some suggestions.

1 Answer

0 votes
by (26.4k points)

Try the following code, just add another dimension to the array.

>>> a = np.array([1, 2, 3])

>>> a

array([1, 2, 3])

>>> a.transpose()

array([1, 2, 3])

>>> a.dot(a.transpose())

14

Now, try to force it to be a column vector

>>> a.shape = (3,1)

>>> a

array([[1],

       [2],

       [3]])

>>> a.transpose()

array([[1, 2, 3]])

>>> a.dot(a.transpose())

array([[1, 2, 3],

       [2, 4, 6],

       [3, 6, 9]])

If want to make a distinction, you can use np.newaxis:

>>> a = np.array([1, 2, 3])

>>> a

array([1, 2, 3])

>>> a[:, np.newaxis]

array([[1],

       [2],

       [3]])

>>> a[np.newaxis, :]

array([[1, 2, 3]]) 

Want to become a Expert in Python? Come and Join: Python course

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer

Browse Categories

...