T
T
Timebird2016-12-01 18:01:16
Python
Timebird, 2016-12-01 18:01:16

How to create an identity matrix in Python if there are NOT ones on the main diagonal?

Hello!
In python, it was required to create an identity matrix, but in such a way that along the main diagonal there were not units, but values ​​from the array (the array is known, it has 80 elements, let's say).
For an ordinary identity matrix, numpy.eye(80) must be applied. What should I do in my case?
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vlad_Fedorenko, 2016-12-01
@Timebird

np.diag([1,2,3])

N
NaName, 2016-12-01
@NaName

#инитиализация нулевой матрицы нужной размерности
matrix = [...][...]
#инитиализация массива значений диагонали
input_massive = [...]
#заполняем диагональ
for i in range(0, len(input_massive)):
    matrix[i][i] = input_massive[i]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question