Max-Abs Scale

EasyMachine LearningMath

Description

Given a vector, scale it by dividing each component by the maximum absolute value so every result lies in [-1, 1]. If the vector is all zeros, return all zeros. Round each result to 4 decimal places.

Examples

Input:[2,-4,1]
Output:[0.5,-1,0.25]
Explanation:

Dividing by the largest magnitude maps the most extreme value to plus or minus one and keeps the rest proportionally inside that band.

Input:[1,2,3]
Output:[0.3333,0.6667,1]
Explanation:

Dividing by the largest magnitude maps the most extreme value to plus or minus one and keeps the rest proportionally inside that band.

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

Dividing by the largest magnitude maps the most extreme value to plus or minus one and keeps the rest proportionally inside that band.

Constraints

  • 1 ≤ length ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.