I need to normalize a vector of N integers so that:
- Each value is proportional to its original value (the value will be between 0 and 1)
- The sum of all values is =1
For instance:
If I have a vector
V = [2,2,1,0]
the normalized vector should should be:
V_norm = [0.4,0.4,0.2,0] % 0.4+0.4+0.2 = 1
I tried with many solutions found in this community and on the web and finally I did it with this code:
part = norm(V);
if part > 0
V_norm = V/part;
else % part = 0 --> avoid "divide by 0"
V_norm = part;
end
The problem this works if:
- all elements of array are “0” –> resultant array doesn’t change
- only one element of the array is >0 and all other elements are = 0 –> resultant array: the element >0 is 1 and the other 0
but if I have a different case,although the result is proportional,the sum is not 0.
For instance:
V = [1,0,1]
V_norm = [0.74,0,0.74]
V = [1,1,1]
V_norm = [0.54,0.54,0.54]
(I’m not sure if the number are correct because I can’t use Matlab right now but I’m sure the sum is > 1 )
Ahy hint?
Thank you in advance
That depends. What is your norm function?
norm(x)in MATLAB returns the standard norm, meaning the sum of the squares of the elements of a normalized vectorxis 1.In your example:
sum(v_norm .^ 2)indeed yields 1, butsum(v_norm)does not, as expected.What do you mean by "normalize"? Does that mean dividing by a value which is a valid mathematical norm function, according to the norm definition?
What do you mean by "proportional"? Does that imply that all elements are multiplied by that same number? If it does, and it’s a valid mathematical norm, you cannot guarantee that the sum of the elements will always be 1.
For instance, consider
v = [1, -2]. Thensum(v) = -1.Or maybe
sumis the function you’re looking for, but it doesn’t mathematically qualify as a norm, because a norm is a function that assigns a strictly positive length or size to all vectors in a vector space.In the example above,
sum(v)is negative.You can choose either:
sum(x), which fulfills both requirements but doesn’t qualify as a norm function since it can yield negative values.norm(x, 1), as OleThomsenBuus suggested, which actually calculatessum(abs(x(:))).It won’t fulfill both your requirements unless you restrict your vector space to non-negative vectors.