Home > Back-end >  Create a matrix using a certain vector in Python
Create a matrix using a certain vector in Python

Time:11-22

I have this vector m = [1,0.8,0.6,0.4,0.2,0] and I have to create the following matrix in Python:

enter image description here

I create a matrix of zeros and a double

mm = np.zeros((6, 6))

for j in list(range(0,6,1)):
    for i in list(range(0,6,1)):
        ind = abs(i-j)
        m[j,i] = mm[ind]

But, I got the following output:

array([[1. , 0.8, 0.6, 0.4, 0.2, 0. ],
   [0.8, 1. , 0.8, 0.6, 0.4, 0.2],
   [0.6, 0.8, 1. , 0.8, 0.6, 0.4],
   [0.4, 0.6, 0.8, 1. , 0.8, 0.6],
   [0.2, 0.4, 0.6, 0.8, 1. , 0.8],
   [0. , 0.2, 0.4, 0.6, 0.8, 1. ]])

That is what I wanted! Thanks anyway.

CodePudding user response:

This could be written by comprehension if you do not want to use numpy,

[m[i::-1]   m[1:len(m)-i]  for i in range(len(m))]
  • Related