1

I would like to get a square matrix B from a linear vector A such that B = A * transpose(A). A is a numpy array and np.shape(A) returns (10,). I would like B to be a (10,10) array. I tried B = np.matmut(A, A[np.newaxis]) but I get an error :

shapes (10,) and (1,10) not aligned: 10 (dim 0) != 1 (dim 0)
1
  • What I want is the equivalent of "B=A*ctranspose(A)" in matlab Commented Nov 23, 2018 at 14:12

3 Answers 3

3

you can do this using outer:

import numpy as np
vector = np.arange(10)
np.outer(vector, vector)
Sign up to request clarification or add additional context in comments.

Comments

2

The solution is a little ugly, but it does what you need.

import numpy as np

vector = np.array([1,2,3,4,5,6,7,8,9,10],)
matrix = np.dot(vector[:,None],vector[None,:])
print(matrix)

You can also do the following:

import numpy as np

vector = np.array([1,2,3,4,5,6,7,8,9,10],)
matrix = vector*vector[:,None]
print(matrix)

The issue comes from the fact that transposing a one dimensional array does not have the effect you might expect.

Comments

0

Variation on outer product:

a = A.reshape(-1, 1) # make sure it's a column vector
B = a @ a.T

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.