0

I am trying to take a diagonal matrix but unfortunately, I am getting an error. My code is:

import numpy as np

a = np.array([1, 2, 3, 2, 1 ,2])
b = np.diagonal((a)*(a).T)
James Z
  • 12,209
  • 10
  • 24
  • 44
jaz
  • 89
  • 1
  • 9

2 Answers2

0

Try

import numpy as np

a = np.array([1, 2, 3, 2, 1 ,2])[np.newaxis]
b = np.diagonal((a)*(a).T)

Refer: Transposing a 1D NumPy array

Ritwik G
  • 406
  • 2
  • 8
0

Or you could do:

import numpy as np

a = np.array([1, 2, 3, 2, 1 ,2]).reshape(1,-1)
b = np.diagonal((a)*(a).T)

The goal is to make this into a matrix of two dimensions rather than a one dimensional array as it was originally declared.

supercooler8
  • 503
  • 2
  • 7