L1 Normalize

EasyMachine LearningMath

Description

Given a vector, scale it so the sum of absolute values equals 1 by dividing each component by the L1 norm. If the vector is all zeros, return all zeros. Round each result to 4 decimal places.

Examples

Input:[1,2,3]
Output:[0.1667,0.3333,0.5]
Explanation:

Dividing every component by the sum of absolute values makes the magnitudes add up to one.

Input:[1,-1]
Output:[0.5,-0.5]
Explanation:

Dividing every component by the sum of absolute values makes the magnitudes add up to one.

Input:[0,0]
Output:[0,0]
Explanation:

Dividing every component by the sum of absolute values makes the magnitudes add up to one.

Constraints

  • 1 ≤ length ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.