http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031
MATLAB Central Newsreader  k (3) Vectors of ones and zeros "combining" together
Feed for thread: k (3) Vectors of ones and zeros "combining" together
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Tue, 17 Jan 2012 23:42:08 +0000
k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864037
Apostol
Hello all!<br>
I would like to generate 3 vectors of a specified length, say N, containing ones and zeros with these constraints:<br>
<br>
all 3 vectors have at least a 1<br>
the sum of these 3 vectors gives ones(1,N), a vector with all ones<br>
<br>
<br>
*this is the only way I think I have for exploring all the possible combinations of a given set of N elements in 3 groups<br>
<br>
Thank you!

Tue, 17 Jan 2012 23:55:09 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864040
Matt J
"Apostol" wrote in message <jf510f$nv5$1@newscl01ah.mathworks.com>...<br>
> Hello all!<br>
> I would like to generate 3 vectors of a specified length, say N, containing ones and zeros with these constraints:<br>
> <br>
> all 3 vectors have at least a 1<br>
> the sum of these 3 vectors gives ones(1,N), a vector with all ones<br>
================<br>
<br>
<br>
A=eye(3,N);<br>
A(3,4:end)=1;<br>
vector1=A(1,:);<br>
vector2=A(2,:);<br>
vector3=A(3,:);

Wed, 18 Jan 2012 00:16:09 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864042
Apostol
"Matt J" wrote in message <jf51ot$q13$1@newscl01ah.mathworks.com>...<br>
<br>
> <br>
> A=eye(3,N);<br>
> A(3,4:end)=1;<br>
> vector1=A(1,:);<br>
> vector2=A(2,:);<br>
> vector3=A(3,:);<br>
<br>
Hi Matt, thanks for you answer! <br>
<br>
but what if I need DIFFERENT combinations? I know for large values of N would be really long/difficult but I would need something that each time generates different combinations so I can evaluate different clusters.<br>
<br>
Thanks :)

Wed, 18 Jan 2012 02:14:08 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864046
Roger Stafford
"Apostol" wrote in message <jf5309$a8$1@newscl01ah.mathworks.com>...<br>
> but what if I need DIFFERENT combinations? I know for large values of N would be really long/difficult but I would need something that each time generates different combinations so I can evaluate different clusters.<br>
> <br>
> Thanks :)<br>
         <br>
This ought to make all possible combinations possible, and they are of equal probability:<br>
<br>
A = zeros(3,n);<br>
p = randperm(N);<br>
q = [1:3,ceil(3*rand(1,N3))];<br>
A(q+3*(p1)) = 1;<br>
<br>
The three rows of A are your three vectors. Each row has at least one 1 and each column has exactly one 1.<br>
<br>
(Note that each time you restart your matlab application it will generate the same sequence of pseudo random numbers unless you use 'randstream' methods.)<br>
<br>
Roger Stafford

Wed, 18 Jan 2012 02:36:08 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864049
Apostol
Thank You very much Roger, I'm trying to use the code you gave me and it seems to work pretty well :)<br>
<br>
<br>
"Roger Stafford" wrote in message <jf59tg$jbs$1@newscl01ah.mathworks.com>...<br>
>          <br>
> This ought to make all possible combinations possible, and they are of equal probability:<br>
> <br>
> A = zeros(3,n);<br>
> p = randperm(N);<br>
> q = [1:3,ceil(3*rand(1,N3))];<br>
> A(q+3*(p1)) = 1;<br>
> <br>
> The three rows of A are your three vectors. Each row has at least one 1 and each column has exactly one 1.<br>
> <br>
> (Note that each time you restart your matlab application it will generate the same sequence of pseudo random numbers unless you use 'randstream' methods.)<br>
> <br>
> Roger Stafford

Wed, 18 Jan 2012 07:13:09 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864058
Roger Stafford
> "Roger Stafford" wrote in message <jf59tg$jbs$1@newscl01ah.mathworks.com>...<br>
> > .......<br>
> > This ought to make all possible combinations possible, and they are of equal probability:<br>
> > .......<br>
       <br>
I retract the claim I made earlier that with this algorithm the various possible combinations will occur with equal probabilities. A large sampling showed that they are not actually equally likely, some types of combinations inherently occurring with greater frequency than others. However all are possible and do occur frequently. Hopefully that is all you need in your work.<br>
<br>
Roger Stafford

Wed, 18 Jan 2012 07:19:08 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864059
Bruno Luong
"Roger Stafford" wrote in message <jf5re5$9lp$1@newscl01ah.mathworks.com>...<br>
<br>
> I retract the claim I made earlier that with this algorithm the various possible combinations will occur with equal probabilities. A large sampling showed that they are not actually equally likely, some types of combinations inherently occurring with greater frequency than others. <br>
<br>
Can you elaborate Roger? It can't feel how the nonequal probability would be possible.<br>
<br>
Bruno

Wed, 18 Jan 2012 09:07:08 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864069
Roger Stafford
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jf5rpc$ah4$1@newscl01ah.mathworks.com>...<br>
> "Roger Stafford" wrote in message <jf5re5$9lp$1@newscl01ah.mathworks.com>...<br>
> <br>
> > I retract the claim I made earlier that with this algorithm the various possible combinations will occur with equal probabilities. A large sampling showed that they are not actually equally likely, some types of combinations inherently occurring with greater frequency than others. <br>
> <br>
> Can you elaborate Roger? It can't feel how the nonequal probability would be possible.<br>
> <br>
> Bruno<br>
         <br>
Like you Bruno, initially my intuition was for equal probabilities. However, after recklessly posting the claim that this is so, I wasn't able to think up a rigorous proof of it and gradually began to doubt its validity. Finally as an act of desperation I ran some empirical tests with millions of samples and found out that, sure enough, the claim simply isn't true. Blush!<br>
<br>
For example, for N = 5, there are two ways of partitioning the five ones into three nonempty subsets, 1 + 1 + 3 and 1 + 2 + 2. There are 60 of the first kind and 90 of the second kind. The probability of each one of the 60 seems to be 1/180 and of the second kind 1/135, adding up to 60/180+90/135=1, (or if it isn't these, they are close.) For larger values of N there are more partition types and thus more distinct probability levels, as is quite evident by their distribution curves.<br>
<br>
I am sure that the theoretical justification of this is there to be found but I am too sleepy to work on it any further tonight.<br>
<br>
Roger Stafford

Wed, 18 Jan 2012 12:29:08 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864087
Roger Stafford
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jf5rpc$ah4$1@newscl01ah.mathworks.com>...<br>
> Can you elaborate Roger? It can't feel how the nonequal probability would be possible.<br>
         <br>
Hi Bruno. Now I can say with certainty that the probabilities are _not_ equal. In the case of N = 5, with the algorithm I used, randperm will select each of the 120 permutations with equal probability. However there are only 60 permutations involving the first 3 elements. A permutation (swap) between the last 2 elements does not affect the statistics of the ceil(3*rand(1,N3)) action, so it would be equivalent to assume that we are using only the first three elements of randperm and taking the remaining 2 in ascending order. For each of these 60 permutations then, there are 9 possible outcomes of ceil(3*rand(1,N3)), of which 3 choose the same value and 6 choose different values. That means of a total of 60*9 equally likely events, 60*3 will be distributed equally over 60 known partitions of the 1+1+3 type and 60*6 will be distributed equally over 90 known partitions of the 1+2+2 <br>
type. This gives a 1/180 probability for each of the first type and 1/135 for each of the second type. The sampling runs I made earlier did in fact indicate the correct probabilities. QED <br>
<br>
For larger N this computation becomes increasingly complicated, so I'll quit now while I'm ahead.<br>
<br>
All of this adds up to the conclusion that this algorithm does not quite achieve the perfection I had in mind for it when I devised it. ;(<br>
<br>
Roger Stafford

Wed, 18 Jan 2012 13:02:08 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864091
Bruno Luong
"Roger Stafford" wrote in message <jf6duk$2gh$1@newscl01ah.mathworks.com>...<br>
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jf5rpc$ah4$1@newscl01ah.mathworks.com>...<br>
> > Can you elaborate Roger? It can't feel how the nonequal probability would be possible.<br>
>          <br>
> Hi Bruno. Now I can say with certainty that the probabilities are _not_ equal. In the case of N = 5, with the algorithm I used, randperm will select each of the 120 permutations with equal probability. However there are only 60 permutations involving the first 3 elements. A permutation (swap) between the last 2 elements does not affect the statistics of the ceil(3*rand(1,N3)) action, so it would be equivalent to assume that we are using only the first three elements of randperm and taking the remaining 2 in ascending order. For each of these 60 permutations then, there are 9 possible outcomes of ceil(3*rand(1,N3)), of which 3 choose the same value and 6 choose different values. That means of a total of 60*9 equally likely events, 60*3 will be distributed equally over 60 known partitions of the 1+1+3 type and 60*6 will be distributed equally over 90 known partitions of the <br>
1+2+2 <br>
> type. This gives a 1/180 probability for each of the first type and 1/135 for each of the second type. The sampling runs I made earlier did in fact indicate the correct probabilities. QED <br>
<br>
I see. Thank Roger for showing the drawback.<br>
<br>
Perhaps that analysis shows the way to fix the original algorithm.<br>
<br>
Bruno

Wed, 18 Jan 2012 15:05:09 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864102
Matt J
"Apostol" wrote in message <jf5309$a8$1@newscl01ah.mathworks.com>...<br>
><br>
> Hi Matt, thanks for you answer! <br>
> <br>
> but what if I need DIFFERENT combinations? I know for large values of N would be really long/difficult but I would need something that each time generates different combinations so I can evaluate different clusters.<br>
===============<br>
<br>
<br>
Here's my next proposal. I've taken trouble neither to optimize it nor to verify that the outcomes are uniformly distributed, but my instinct tells me it should be.<br>
<br>
<br>
N=10;<br>
A=zeros(3,N);<br>
<br>
<br>
%%Gaurantee at least one 1 in each row via sampling w/o replacement<br>
Nset=1:N;<br>
for ii=N:1:N2<br>
Nset(randi(ii,1))=[];<br>
end<br>
<br>
jj=setdiff(1:N,Nset);<br>
<br>
for ii=1:3<br>
A(ii,jj(ii))=1; <br>
end<br>
<br>
%%Now fill zerocolumns arbitrarily<br>
<br>
<br>
idx=sub2ind([3,N],randi(3,[1,N3]),Nset);<br>
A(idx)=1;

Wed, 18 Jan 2012 18:07:11 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864115
Matt J
"Matt J" wrote in message <jf6n35$3kn$1@newscl01ah.mathworks.com>...<br>
> "Apostol" wrote in message <jf5309$a8$1@newscl01ah.mathworks.com>...<br>
> ><br>
> > Hi Matt, thanks for you answer! <br>
> > <br>
> > but what if I need DIFFERENT combinations? I know for large values of N would be really long/difficult but I would need something that each time generates different combinations so I can evaluate different clusters.<br>
> ===============<br>
> <br>
> <br>
> Here's my next proposal. I've taken trouble neither to optimize it nor to verify that the outcomes are uniformly distributed, but my instinct tells me it should be.<br>
> <br>
> <br>
=================<br>
<br>
Forget it. I now realize that my approach is equivalent to Roger's...

Wed, 18 Jan 2012 23:36:10 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864145
Apostol
"Matt J" wrote in message <jf71oe$bbi$1@newscl01ah.mathworks.com>...<br>
> "Matt J" wrote in message <jf6n35$3kn$1@newscl01ah.mathworks.com>...<br>
> > "Apostol" wrote in message <jf5309$a8$1@newscl01ah.mathworks.com>...<br>
> > ><br>
> > > Hi Matt, thanks for you answer! <br>
> > > <br>
> > > but what if I need DIFFERENT combinations? I know for large values of N would be really long/difficult but I would need something that each time generates different combinations so I can evaluate different clusters.<br>
> > ===============<br>
> > <br>
> > <br>
> > Here's my next proposal. I've taken trouble neither to optimize it nor to verify that the outcomes are uniformly distributed, but my instinct tells me it should be.<br>
> > <br>
> > <br>
> =================<br>
> <br>
> Forget it. I now realize that my approach is equivalent to Roger's...<br>
<br>
<br>
Thank you very much guys! For small values of N this shouldn't be a problem, given that for my specific problem some groups have the same "value" of others... if it'll get tougher with a higher N perhaps I should consider other options. <br>
Thank You Again! :)

Thu, 19 Jan 2012 18:28:10 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864235
Matt J
"Apostol" wrote in message <jf7l1a$h8i$1@newscl01ah.mathworks.com>...<br>
><br>
> > Forget it. I now realize that my approach is equivalent to Roger's...<br>
> <br>
> <br>
> Thank you very much guys! For small values of N this shouldn't be a problem, given that for my specific problem some groups have the same "value" of others... if it'll get tougher with a higher N perhaps I should consider other options. <br>
===================<br>
<br>
Below is a method which I really do believe should give uniformly distributed results, but for some reason it is giving me some weird outliers in my histogram plots for N=5 and 1e6 samples. Possibly 1e6 is still insufficient...?<br>
<br>
<br>
sets=nchoosek(1:N1,2);<br>
M=size(sets,1);<br>
thisSet=sets(randi(M),:);<br>
len1=thisSet(1);<br>
len2=thisSet(2)thisSet(1);<br>
len3=N(len1+len2);<br>
<br>
A=[repmat([1;0;0],1,len1),...<br>
repmat([0;1;0],1,len2),... <br>
repmat([0;0;1],1,len3)]; <br>
<br>
A=A(:,randperm(N));

Sun, 22 Jan 2012 19:36:24 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864340
Roger Stafford
"Matt J" wrote in message <jf9nbq$b9h$1@newscl01ah.mathworks.com>...<br>
> Below is a method which I really do believe should give uniformly distributed results, but for some reason it is giving me some weird outliers in my histogram plots for N=5 and 1e6 samples. Possibly 1e6 is still insufficient...?<br>
> <br>
> sets=nchoosek(1:N1,2);<br>
> M=size(sets,1);<br>
> thisSet=sets(randi(M),:);<br>
> len1=thisSet(1);<br>
> len2=thisSet(2)thisSet(1);<br>
> len3=N(len1+len2);<br>
> A=[repmat([1;0;0],1,len1),...<br>
> repmat([0;1;0],1,len2),... <br>
> repmat([0;0;1],1,len3)]; <br>
> A=A(:,randperm(N));<br>
         <br>
Matt, if I understand your code correctly, for N = 5 the 'randi' instruction gives the "1+1+3" types equal probability with the "1+2+2" types (that is, three of each) before permutations with 'randperm' occur. Out of 243 possible arrangements of A with one 1 in each column there are only 150 of them that also have at least one 1 in each row. Unfortunately, of these there are only of the 1+1+3 type as against 90 of the 1+2+2 type after permutations are done. Hence the latter are individually less probable than the former. You would need to have somehow made the initial probability assignments for these two types before permutation to be in the ratio of 2 to 3 for it to come out right.<br>
<br>
I think I can see how it might be done in the general case, but it would require a more extensive calculation.<br>
<br>
Roger Stafford

Sun, 22 Jan 2012 20:45:10 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864427
Roger Stafford
"Roger Stafford" wrote in message <jfhofo$l7e$1@newscl01ah.mathworks.com>...<br>
> Matt, if I understand your code correctly, for N = 5 the 'randi' instruction gives the "1+1+3" types equal probability with the "1+2+2" types (that is, three of each) before permutations with 'randperm' occur. Out of 243 possible arrangements of A with one 1 in each column there are only 150 of them that also have at least one 1 in each row. Unfortunately, of these there are only of the 1+1+3 type as against 90 of the 1+2+2 type after permutations are done. Hence the latter are individually less probable than the former. You would need to have somehow made the initial probability assignments for these two types before permutation to be in the ratio of 2 to 3 for it to come out right.<br>
       <br>
The number '60' got lost in my text. It should read: "Unfortunately, of these there are only 60 of the 1+1+3 type as against 90 of the 1+2+2 type after permutations are done."<br>
<br>
Roger Stafford

Sun, 22 Jan 2012 21:10:10 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864428
Matt J
"Roger Stafford" wrote in message <jfhsgm$3bj$1@newscl01ah.mathworks.com>...<br>
><br>
> The number '60' got lost in my text. It should read: "Unfortunately, of these there are only 60 of the 1+1+3 type as against 90 of the 1+2+2 type after permutations are done."<br>
=====================<br>
<br>
I'm failing to see why that's true, Roger. As far as I can tell, every matrix of the 1+1+3 type must be a permutation of the columns of<br>
A113=[1 0 0 0 0; 0 1 0 0 0 ; 0 0 1 1 1]<br>
in order for the rows to sum to 1. Hence there ought to be N! of them<br>
<br>
Similarly, every matrix of the 1+2+2 type must be a permutation of the columns of<br>
A122=[1 0 0 0 0; 0 1 1 0 0 ; 0 0 0 1 1]<br>
There ought to be N! of them as well.

Sun, 22 Jan 2012 21:44:09 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864429
Matt J
"Matt J" wrote in message <jfhtvi$7i7$1@newscl01ah.mathworks.com>...<br>
> "Roger Stafford" wrote in message <jfhsgm$3bj$1@newscl01ah.mathworks.com>...<br>
> ><br>
> > The number '60' got lost in my text. It should read: "Unfortunately, of these there are only 60 of the 1+1+3 type as against 90 of the 1+2+2 type after permutations are done."<br>
> =====================<br>
> <br>
> I'm failing to see why that's true, Roger. As far as I can tell, every matrix of the 1+1+3 type must be a permutation of the columns of<br>
> A113=[1 0 0 0 0; 0 1 0 0 0 ; 0 0 1 1 1]<br>
> in order for the rows to sum to 1. Hence there ought to be N! of them<br>
=============<br>
<br>
OK, I think I see now why that's not true. Reordering the final 3 columns produces no new distinct matrices. Hmmm....

Sun, 22 Jan 2012 23:37:09 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864435
Matt J
"Roger Stafford" wrote in message <jfhofo$l7e$1@newscl01ah.mathworks.com>...<br>
><br>
> I think I can see how it might be done in the general case, but it would require a more extensive calculation.<br>
==========<br>
<br>
I think this next version does the job, finally. I'm seeing very uniform looking histograms for 1e7 samples.<br>
<br>
<br>
%This next bit should be computed only once per N<br>
<br>
sets=nchoosek(1:N1,2);<br>
Lengths=[sets(:,1), sets(:,2)sets(:,1), Nsets(:,2)];<br>
redundancies=1./prod(factorial(Lengths),2);<br>
cdf=cumsum(redundancies);<br>
cdf=[0;cdf].'/cdf(end);<br>
<br>
<br>
%Engine<br>
<br>
n=find(histc(rand,cdf),1);<br>
<br>
<br>
len1=Lengths(n,1);<br>
len2=Lengths(n,2);<br>
len3=Lengths(n,3);<br>
<br>
A=[repmat([1;0;0],1,len1),...<br>
repmat([0;1;0],1,len2),... <br>
repmat([0;0;1],1,len3)]; <br>
<br>
A=A(:,randperm(N));

Mon, 23 Jan 2012 05:30:11 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864476
Roger Stafford
"Matt J" wrote in message <jfi6j5$2dv$1@newscl01ah.mathworks.com>...<br>
> sets=nchoosek(1:N1,2);<br>
> Lengths=[sets(:,1), sets(:,2)sets(:,1), Nsets(:,2)];<br>
> redundancies=1./prod(factorial(Lengths),2);<br>
> cdf=cumsum(redundancies);<br>
> cdf=[0;cdf].'/cdf(end);<br>
> %Engine<br>
> n=find(histc(rand,cdf),1);<br>
> len1=Lengths(n,1);<br>
> len2=Lengths(n,2);<br>
> len3=Lengths(n,3);<br>
> A=[repmat([1;0;0],1,len1),...<br>
> repmat([0;1;0],1,len2),... <br>
> repmat([0;0;1],1,len3)]; <br>
> A=A(:,randperm(N));<br>
        <br>
That code looks good to me, Matt. I haven't tried making a histogram of it, but the theory looks perfectly valid. I congratulate you.<br>
<br>
I found it easiest to think of by temporarily allowing zero length counts. Each sequence of length counts a, b, c where a+b+c = N can occur in N!/(a!*b!*c!) ways among the 3^N, presumably equally probable, possibilities, and sum of these will be exactly 3^N. The probability of each possible sequence should therefore be set proportional to 1/(a!*b!*c!) to accomplish this, which is what you have done. Restricting things to nonzero length counts doesn't change that needed proportionality, (even though the above sum would now be a smaller 3^N3*(2^N1).)<br>
<br>
About the only problem you might encounter occurs with large N. In those cases it gets a little precarious depending on only one 'rand' call to distinguish between vastly differing probabilities. At the extreme of N = 45 some of the lowest probabilities would amount to only about the value of one bit in a 53bit floating point number, so they would be highly inaccurate relative to their own size. (Of course happily they wouldn't occur very often.)<br>
<br>
Roger Stafford

Mon, 23 Jan 2012 15:16:11 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864536
Matt J
"Roger Stafford" wrote in message <jfir93$11i$1@newscl01ah.mathworks.com>...<br>
><br>
> That code looks good to me, Matt. I haven't tried making a histogram of it, but the theory looks perfectly valid. I congratulate you.<br>
> <br>
> I found it easiest to think of by temporarily allowing zero length counts. Each sequence of length counts a, b, c where a+b+c = N can occur in N!/(a!*b!*c!) ways among the 3^N, presumably equally probable, possibilities, and sum of these will be exactly 3^N. The probability of each possible sequence should therefore be set proportional to 1/(a!*b!*c!) to accomplish this, which is what you have done. Restricting things to nonzero length counts doesn't change that needed proportionality, (even though the above sum would now be a smaller 3^N3*(2^N1).)<br>
=================<br>
<br>
Thanks, Roger, although I am a little disquieted that it takes 1e7 realizations to get a decently converged histogram. I would think that with only 150 different possible events, the number of required samples would be far less...<br>
<br>
<br>
<br>
> About the only problem you might encounter occurs with large N. In those cases it gets a little precarious depending on only one 'rand' call to distinguish between vastly differing probabilities. At the extreme of N = 45 some of the lowest probabilities would amount to only about the value of one bit in a 53bit floating point number, so they would be highly inaccurate relative to their own size. (Of course happily they wouldn't occur very often.)<br>
=======<br>
<br>
True. But since the OP didn't specify how large N can get, I'm inclined not to let it trouble me.

Mon, 23 Jan 2012 21:37:51 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864570
Apostol
"Matt J" wrote in message <jfjtjr$fij$1@newscl01ah.mathworks.com>...<br>
<br>
> True. But since the OP didn't specify how large N can get, I'm inclined not to let it trouble me.<br>
<br>
<br>
Thank you very much again guys! For a limitcase problem I shouldn't exceed N=20, the code you provided is far better than anything I could have thought of. It's very frustrating that I have to run this many iterations to have a good coverage of all the different combinations, anyway as I said this is perfect for me!

Mon, 23 Jan 2012 22:01:10 +0000
Re: k (3) Vectors of ones and zeros "combining" together
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316031#864585
Roger Stafford
"Matt J" wrote in message <jfjtjr$fij$1@newscl01ah.mathworks.com>...<br>
> Thanks, Roger, although I am a little disquieted that it takes 1e7 realizations to get a decently converged histogram. I would think that with only 150 different possible events, the number of required samples would be far less...<br>
> ........<br>
          <br>
It's not so surprising, Matt. For N = 5, assuming that all 150 events have equal probability p = 1/150, what you have with n samples is a simple binomial distribution with mean n*p and variance n*p*(1p) on the count of one of these events. The right quantity to consider in its histogram is the ratio sqrt(variance)/mean which is equal to sqrt((1p)/(n*p)). As n increases, it drops down only as the reciprocal of the square root of n and the small size of p makes it worse. For larger N the number of events increases and therefore the number of samples needed gets even larger. Also one needs to take into consideration on a histogram that with larger numbers of events, there is an increased opportunity for outliers to appear even for a given value of the above ratio.<br>
<br>
Roger Stafford