简体   繁体   中英

How to normalize a vector with negative values using matlab?

For a given vector, say v=[1 2 2] , I need to normalize and the sum of all values in the resultant matrix must be 1. Then I am using the matlab code as w=v/norm(v,1) . Now the result w=[0.2000 0.4000 0.4000] ie sum=0.2+0.4+0.4=1 and the condition is satisfied. But when using a negative value, the result is wrong. ie if v=[1 -2 2] and w=v/norm(v,1) . Now the result is w=[0.2000 -0.4000 0.4000] and sum = 0.2+(-0.4)+0.4 != 1 .This sum is not equal to one. Then am using w=abs(v)/norm(v,1) . Is this correct?

I would consider subtracting the lowest value:

V = v - min(v)
W = V/norm(V,1)

now sum(W) does equal 1 and you won't be discarding information like you would if you use abs

norm(v,1) ignores sign (it computes sum(abs(v)) ). To make the sum of the vector equal 1, you could use

w = v/sum(v);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM