kechap
kechap

Reputation: 2087

Construct a vector with sum of all elements to be 1

I am looking for a quick way to construct a vector of size n.

The sum of all its elements must be 1.

For example if n = 5 the vector should be v = [0.1 0.3 0.03 0.07 0.5].

Any ideas on how to do that?

Upvotes: 0

Views: 305

Answers (2)

Boris
Boris

Reputation: 7132

One simple possibility is

v=rand(5,1); v=v/sum(v)

Upvotes: 4

Nzbuu
Nzbuu

Reputation: 5251

Just create the vector and normalise it:

v = v ./ sum(v);

Upvotes: 0

Related Questions