-1

i have to make three vectors with probabilities p1, p2 and p3= (1-p1-p2) in order to plot the entropy of a signal source without memory that produces three symbols. I have tried many things with rand() and vectors like [1: .001:1] but none worked as it supposed to, the main problem is that i can't maintain a constant sum of one for every set of probabilities. Is there any way or funtion to do this? Any piece of advice would be appreciated.

1 Answers1

2

You can generate a vector p of 3 real numbers between zero and one

p = rand(1,3);

then normalize p

p = p / sum(p);

Then p(1) + p(2) + p(3) is 1.

EDIT:

to respond to OP's comment

N = 100;

p = rand(N, 3);
for k = 1: N
    p(k,:) = p(k,:)/sum(p(k,:));
end;

Now you have an N x 3 matrix with rows summing up to one. I'm pretty sure that there is a more "pure" MATLAB way of writing the loop using some vectorization tricks, however my MATLAB is kind of rusty now, and this will do. Please next time be more precise when asking a question.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • I was going to go nuts because the use of `norm` but you were faster hahah. Good job ;). +1 – Ander Biguri Dec 03 '14 at 17:03
  • 1
    @AnderBiguri quantum mechanics habit of using `norm` :) – vsoftco Dec 03 '14 at 17:04
  • Thanks for responding. i need to make three different vectors p1,p2 and p3 with 100 columns each, moreover the sum of every set should be 1 ( p1(1)+p2(1)+p3(1)= 1, p1(2)+p2(2)+p3(2)=1 ) in order to plot entropy vs p1,p2 and p3. – MESOLOGGITIS IOANNIS Dec 03 '14 at 17:11
  • 1
    @vsoftco I think the "more pure Matñab way" you refer to is to replace the loop by `p = bsxfun(@rdivide, p, sum(p,2));` – Luis Mendo Dec 03 '14 at 17:41
  • @LuisMendo thanks :) I couldn't have come up with that by myself :) – vsoftco Dec 03 '14 at 17:45
  • 1
    I've upvoted this because it seems to satisfy the question, which wasn't very clear anyway :-) But if one desires the `p` values to be _uniformly distributed_ (i.e. all values equally likely), this approach is not suitable. See [here](http://stackoverflow.com/questions/8064629/random-numbers-that-add-to-100-matlab) – Luis Mendo Dec 03 '14 at 17:48
  • your code helped but i still have a problem when i have to plot it because the numbers are not sorted: for k = 1: N p(:,k) = p(:,k)/sum(p(:,k)); end; for k = 1: N p1(k) = p(1,k); p2(k) = p(2,k); p3(k) = p(3,k); y(k) = -p1(k)*log2(p1(k))-p2(k)*log2(p2(k))-p3(k)*log2(p3(k)); end plot3(y,p1,p2) – MESOLOGGITIS IOANNIS Dec 03 '14 at 18:12
  • @MESOLOGGITISIOANNIS just construct a vector with the entropy of each row, sort the vector, then plot it if that's what you want. – vsoftco Dec 03 '14 at 18:15