Jaya
Jaya

Reputation: 81

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] i.e. sum=0.2+0.4+0.4=1 and the condition is satisfied. But when using a negative value, the result is wrong. i.e. 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?

Upvotes: 3

Views: 5020

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112749

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);

Upvotes: 0

Dan
Dan

Reputation: 45752

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

Upvotes: 0

Related Questions