0

When I multiply an nxn matrix by a nx1 vector, I get back a nested vector. How do I avoid this or how do I recover the vector.

Examples:

Using @ operator

A = np.matrix([[1,4,3],[2,2,1],[5,4,2]])
v = np.array([1,2,3])
A @ v => RuntimeError: Iterator automatic output has an array subtype which changed the dimensions of the output

Using np.dot

np.dot(A,v) => matrix([[18,9,19]])

Here the vector is nested and is a matrix of shape (3,1). I just want a vector of length 3 (i.e. of shape (3,))

If I had declared the matrix as an array, using the @ operator would've worked just fine, but I can't do this for this homework exercise.

The Bosco
  • 196
  • 12
  • `np.matrix` is ALWAYS 2d. And these math operations using `np.matrix` return `np.matrix`, even if the other argument is `ndarray`. You can use `.A1` to return a 1d `ndarray` from a `np.matrix`: `np.dot(A,v).A1`. – hpaulj Nov 02 '18 at 06:51
  • Why is this homework assignment forcing you to use `np.matrix`? Read the docs. Developers would like us to stop using it. I"m closing this because the `matrix to array` conversion has already been covered. – hpaulj Nov 02 '18 at 06:53
  • Thanks! The object I given work with is a matrix. I don't know why, since they expect us to produce an array – The Bosco Nov 02 '18 at 07:02

0 Answers0