Skip to content
DeepForgeby svx
All problems
EasyLinear Algebrala-103

Diagonal Matrix from Vector

Problem

Build the n x n diagonal matrix whose diagonal is the vector v.

Return D with D[i][j] = v[i] if i == j and 0 otherwise.

Hint

This is the inverse operation of extracting the main diagonal.

Test cases

case 1
in: [[1,2,3]]
expected: [[1,0,0],[0,2,0],[0,0,3]]
case 2
in: [[5]]
expected: [[5]]
case 3
in: [[0,0]]
expected: [[0,0],[0,0]]
case 4
in: [[1.5,-2.5]]
expected: [[1.5,0],[0,-2.5]]
Starter codePython
def diag_matrix(v):
    # Your code here
    pass

4 test cases · run your code against them in the editor