Eddie
Eddie

Reputation: 119

Normalize a vector?

How do you normalize a M*N vector, such that the sum of all its elements is now equal to 1. I browsed online a little, and nothing seems to quite match what I need. Thanks!

Upvotes: 2

Views: 4423

Answers (2)

maria
maria

Reputation: 467

i think you have to divide every vector component by the euklidean distance of the vector

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490098

You add up all the elements, then divide each element by the sum.

Obviously, the division (at least) needs to be in floating point. Since that indicates a floating point matrix, doing the summing while maintaining maximum accuracy will be non-trivial.

Just for example, if you have one large element, and a lot of small elements, you'll probably get a more accurate result from adding all the small elements together, then adding that sum to the large element, than if you added each small element to the large one individually.

Edit: I suppose I should add that the usual way to deal with this is called Kahan summation, after the high guru of numerical analysis, William Kahan.

Upvotes: 1

Related Questions