Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Normalize to Unity?

617 views
Skip to first unread message

Zach Weiner

unread,
Sep 23, 2008, 8:07:01 PM9/23/08
to
I have a function in an if-end loop, which is generating a column matrix. I need to normalize it to unity. Anyone know how to do this?

Walter Roberson

unread,
Sep 23, 2008, 8:22:53 PM9/23/08
to
Zach Weiner wrote:
> I have a function in an if-end loop, which is generating a column matrix. I
> need to normalize it to unity. Anyone know how to do this?

There is more than one meaning of "normalize". Are you talking about the
standard deviation? Or do you mean something like:

N = norm(TheColumnVector);
if N == 0
error('Uh-oh, asked to normalize the null vector!');
end
NormalizedColumnVector = TheColumnVector ./ N;

Zach Weiner

unread,
Sep 23, 2008, 8:31:20 PM9/23/08
to
By normalize, I mean that if you summed all the values, the resulting amount would be 1.

John D'Errico

unread,
Sep 23, 2008, 9:24:01 PM9/23/08
to
"Zach Weiner" <zach_...@yahoo.com> wrote in message <gbc1on$74g$1...@fred.mathworks.com>...

> By normalize, I mean that if you summed all the values, the resulting amount would be 1.


So you have essentially a vector. You want the sum
of the vector to be 1. It has some other sum.

There are two simple ways to do this, given a
vector v,

v = rand(10,1);
sum(v)
ans =
6.2386

As you can see, v does not sum to 1.

You can either translate each element of v by an
amount,

v1 = v + (1 - sum(v))/length(v);
sum(v1)
ans =
1

or you can scale the vector.

v2 = v/sum(v);
sum(v2)
ans =
1

Either way will work. It depends on how you will
choose to define "normalize".

If you wish to do the nomialization on the entire
array after it has been generated, use the above
tricks in conjunction with bsxfun.

HTH,
John

0 new messages