I need to normalize a vector of N integers so that:
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:
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
To normalize a vector, therefore, is to take a vector of any length and, keeping it pointing in the same direction, change its length to 1, turning it into what is called a unit vector. Since it describes a vector's direction without regard to its length, it's useful to have the unit vector readily accessible.
N = normalize( A ) returns the vectorwise z-score of the data in A with center 0 and standard deviation 1. If A is a vector, then normalize operates on the entire vector A . If A is a matrix, then normalize operates on each column of A separately.
Normalisation by sum: each value in a row (sample) is divided by the total sum of the row (sample) and multiplied by 100; the unit is %. PQN: for every feature the mean response is calculated across all QC samples. A reference vector is then generated.
Unit Vectors - Normalizing Normalizing a vector involves two steps: 1 calculate its length, then, 2 divide each of its (xy or xyz) components by its length.
What you need to do is, I believe, normalize using the 1-norm (taxicab norm):
v = [2, 2, 1, 0];
v_normed = v / norm(v, 1); % using the 1-norm
Variable v_normed
should now be [0.4, 0.4, 0.2, 0.0]
. The 1-norm of v_normed
will equal 1. You can also sum the vector (similar to the 1-norm, but without applying the absolute function to each value), but the range of that sum will be between -1 to 1 in the general case (if any values in v
are below 0). You could use abs
on the resulting sum, but mathematically it will no longer qualify as a norm.
... the normalized vector should should be:
v_norm = [0.4, 0.4, 0.2, 0]; % 0.4+0.4+0.2 = 1
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 vector x
is 1.
In your example:
v = [1, 1, 1]; %# norm(v) = sqrt(1^2+1^2+1^2) = ~1.7321
v_norm = v / norm(v); %# v_norm = [0.5574, 0.5574, 0.5574]
sum(v_norm .^ 2)
indeed yields 1, but sum(v_norm)
does not, as expected.
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) and the sum of all values is 1.
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]
. Then sum(v) = -1
.
Or maybe sum
is 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.
Ahy hint?
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 calculates sum(abs(x(:)))
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With