如何在Matlab中使一个向量标准化,其中的和为1?

时间:2020-12-06 04:18:44

I need to normalize a vector of N integers so that:

我需要使一个N个整数的向量标准化,这样:

  • Each value is proportional to its original value (the value will be between 0 and 1)
  • 每个值与它的原始值成比例(值将在0和1之间)
  • The sum of all values is =1
  • 所有值之和为=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
  • 数组的所有元素都是“0”—>结果数组不变。
  • 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
  • 数组中只有一个元素是>0,所有其他元素都是= 0——>合成数组:元素>0是1,另一个是0。

but if I have a different case,although the result is proportional,the sum is not 0. For instance:

但是如果我有一个不同的情况,尽管结果是成比例的,总和不是0。例如:

   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 )

(我不确定这个数字是否正确,因为我现在不能用Matlab,但我肯定这个数字是> 1)

Ahy hint?

Ahy提示?

Thank you in advance

提前谢谢你

3 个解决方案

#1


9  

... the normalized vector should should be:

…归一化向量应该是:

v_norm = [0.4, 0.4, 0.2, 0]; % 0.4+0.4+0.2 = 1

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.

在MATLAB中,norm(x)返回标准规范,即标准化向量x的元素平方和为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.

总和(v_norm。^ 2)实际上收益率1,但总和(v_norm)并不像预期的那样。


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.

我需要使一个N个整数的向量标准化,使每个值与它的原始值成比例(值将在0和1之间),所有值的和为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.

你说的“比例”是什么意思?这是否意味着所有的元素都乘以相同的数?如果是,这是一个有效的数学准则,你不能保证元素的和总是1。例如,考虑v =[1, -2]。然后求和(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.

或者求和是你要找的函数,但它在数学上不符合标准,因为标准是一个函数,它给向量空间中的所有向量分配一个严格的正长度或大小。在上面的例子中,sum(v)是负的。


Ahy hint?

Ahy提示?

You can choose either:

你可以选择:

  1. sum(x), which fulfills both requirements but doesn't qualify as a norm function since it can yield negative values.
  2. sum(x)满足这两种需求,但不符合标准函数,因为它可以产生负值。
  3. norm(x, 1), as OleThomsenBuus suggested, which actually calculates sum(abs(x(:))).
    It won't fulfill both your requirements unless you restrict your vector space to non-negative vectors.
  4. norm(x, 1),正如OleThomsenBuus所建议的,实际上是计算和(abs(x(:)))。除非你把向量空间限制为非负的向量,否则它不会满足你的要求。

#2


9  

What you need to do is, I believe, normalize using the 1-norm (taxicab norm):

你需要做的是,我相信,用1规范(出租车规范)来规范:

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.

变量v_normed现在应该是[0.4,0.4,0.2,0.0]。v_normed的1-范数等于1。还可以和向量(类似于1-norm,但是没有绝对函数应用到每个值),但范围和在一般情况下会在1到1之间(如果任何值v低于0)。你可以用abs产生的总和,但数学将不再成为常态。

#3


6  

If there are no furhter conditions to your normalization than you gave at the beginning of your question, a possible solution would be

如果在你的问题开始时,你的标准化没有任何的变化,那么一个可能的解决方案就是。

V = [3 4 -2];
S = sum(V);
if (S == 0)
    % no solution
else
    V_norm = V ./ S;
end
sum(V_norm)

#1


9  

... the normalized vector should should be:

…归一化向量应该是:

v_norm = [0.4, 0.4, 0.2, 0]; % 0.4+0.4+0.2 = 1

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.

在MATLAB中,norm(x)返回标准规范,即标准化向量x的元素平方和为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.

总和(v_norm。^ 2)实际上收益率1,但总和(v_norm)并不像预期的那样。


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.

我需要使一个N个整数的向量标准化,使每个值与它的原始值成比例(值将在0和1之间),所有值的和为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.

你说的“比例”是什么意思?这是否意味着所有的元素都乘以相同的数?如果是,这是一个有效的数学准则,你不能保证元素的和总是1。例如,考虑v =[1, -2]。然后求和(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.

或者求和是你要找的函数,但它在数学上不符合标准,因为标准是一个函数,它给向量空间中的所有向量分配一个严格的正长度或大小。在上面的例子中,sum(v)是负的。


Ahy hint?

Ahy提示?

You can choose either:

你可以选择:

  1. sum(x), which fulfills both requirements but doesn't qualify as a norm function since it can yield negative values.
  2. sum(x)满足这两种需求,但不符合标准函数,因为它可以产生负值。
  3. norm(x, 1), as OleThomsenBuus suggested, which actually calculates sum(abs(x(:))).
    It won't fulfill both your requirements unless you restrict your vector space to non-negative vectors.
  4. norm(x, 1),正如OleThomsenBuus所建议的,实际上是计算和(abs(x(:)))。除非你把向量空间限制为非负的向量,否则它不会满足你的要求。

#2


9  

What you need to do is, I believe, normalize using the 1-norm (taxicab norm):

你需要做的是,我相信,用1规范(出租车规范)来规范:

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.

变量v_normed现在应该是[0.4,0.4,0.2,0.0]。v_normed的1-范数等于1。还可以和向量(类似于1-norm,但是没有绝对函数应用到每个值),但范围和在一般情况下会在1到1之间(如果任何值v低于0)。你可以用abs产生的总和,但数学将不再成为常态。

#3


6  

If there are no furhter conditions to your normalization than you gave at the beginning of your question, a possible solution would be

如果在你的问题开始时,你的标准化没有任何的变化,那么一个可能的解决方案就是。

V = [3 4 -2];
S = sum(V);
if (S == 0)
    % no solution
else
    V_norm = V ./ S;
end
sum(V_norm)