Skip to content
DeepForgeby svx
All problems
EasyLinear Algebrala-106

Normalize Matrix Rows

Problem

Normalize every row of matrix A to unit L2 length.

If a row is the zero vector, leave it as zeros. Return a list of rows of floats.

Hint

Treat each row as an independent vector and reuse the L2 norm formula.

Test cases

case 1
in: [[[3,4],[0,0]]]
expected: [[0.6,0.8],[0,0]]
case 2
in: [[[1,0],[0,1]]]
expected: [[1,0],[0,1]]
case 3
in: [[[1,1]]]
expected: [[0.7071067811865475,0.7071067811865475]]
case 4
in: [[[0,0],[5,0]]]
expected: [[0,0],[1,0]]
Starter codePython
def normalize_rows(A):
    # Your code here
    pass

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