Hi, I'm looking for a solution for my problem, I'm trying to generate a random vector of nine elements with fixed sum of 3, but they have to be naturals, from 0 - 3. I tried to modify the following file with no success.

"Pablo NB" wrote in message <jm32ie$8me$1@newscl01ah.mathworks.com>...
> Hi, I'm looking for a solution for my problem, I'm trying to generate a random vector of nine elements with fixed sum of 3, but they have to be naturals, from 0 - 3.

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jm39qu$2mj$1@newscl01ah.mathworks.com>...
> diff([0 sort(floor(4*rand(1,8))) 3])
- - - - - - - - - -
Using diff/sort in that manner does have a statistical shortcoming in my opinion, Bruno. One way to illustrate this is the following. The probabilities of each of the 3^8 = 6561 possible results of

floor(4*rand(1,8))

are equal. Out of these, just one of them is 3 3 3 3 3 3 3 3, but seventy of them give four 3's and four 0's. After the 'sort' followed by 'diff' the final results of these two cases are

3 0 0 0 0 0 0 0 0

and

0 0 0 0 3 0 0 0 0

respectively, and this is the only way to produce these two final results. Thus, the second of these would be seventy times more likely than the first with this method of generation.

Out of 3^9 = 19683 possible sequences of nine integers from 0 to 3, there are only 165 that yield a sum of 3, nine of them with a single 3, seventy-two of them with a single 1 and a single 2, and 84 of them with three 1's, all the rest in each case being a 0. It would seem best that each of these should be of equal probability, namely 1/165, in such a random generation.

One way is to attack the problem with a brute force method such as the following:

s = zeros(9,1);
r = ceil(165*rand);
if r <= 9 % Set a single element to 3
s(ceil(9*rand)) = 3;
else
t = randperm(9,3);
if r <= 9+72 % Set one element to 1 and another to 2
s(t(1:2)) = [1;2];
else % Set three elements to 1
s(t) = 1;
end
end

The result will be in the sequence, s.

Pablo, if you had in mind generalizing this problem, a more clever way of doing it than this would have to be devised. As you found, my own 'randfixedsum' routine would be of no use to you since it is designed for values within a real continuum. My intuition is that your problem in a general form would possess a similar complexity to 'randfixedsum' and therefore not easily forthcoming, though perhaps I am being too pessimistic.

"Roger Stafford" wrote in message <jm5mn3$g70$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jm39qu$2mj$1@newscl01ah.mathworks.com>...
> > diff([0 sort(floor(4*rand(1,8))) 3])
> - - - - - - - - - -
> Using diff/sort in that manner does have a statistical shortcoming in my opinion, Bruno. One way to illustrate this is the following. The probabilities of each of the 3^8 = 6561 possible results of
>
> floor(4*rand(1,8))

Do you mean 4^8 = 65538 ?

>
> are equal. Out of these, just one of them is 3 3 3 3 3 3 3 3,

OK

> but seventy of them give four 3's and four 0's.

Do you mean the chance to get [3 3 3 3 0 0 0 0]? To me it equals to 1/4^8, just like [3 3 3 3 3 3 3 3] . I honestly can't see why the seventy comes from Roger. There is no permutation in my code.

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jm5u4u$av2$1@newscl01ah.mathworks.com>...
> Waht about this algo:
>
> L1 = 3;
> n = 9;
>
> s = sort(randperm(n+L1-1,n-1));
> v = diff([0 s n+L1]) - 1
>
> Bruno
- - - - - - - - - -
That's a great algorithm, Bruno! The "sort(randperm(p,q))" step has the interesting property of generating with equal probability all possible subsequences of 1:p possessing q elements. In the current problem with p = 11 and q = 8, there are 165 such subsequences. The step of "diff([0,s,p+1])" establishes a one-to-one relationship between these subsequences and the possible vectors of q+1 integers greater than or equal to 1 whose sum is p+1. By subtracting 1 we have q+1 whole numbers (>=0) whose sum is p-q, which gives the solution to the current problem.

You caught me in an error, Bruno. It should indeed be 4^8 = 65536 and 4^9 = 262144. Fortunately for me, the 9+72+84 = 165 was still correct so the code I gave should be valid. However, yours is better, Bruno. It generalizes to any n non-negative integers whose sum is s.

My use of the term "general form" referred to the case of n non-negative integers restricted to the range 0:m whose sum is s, which I believe is a much harder case that probably cannot be vectorized. Maybe you can prove me wrong on this, Bruno.

"Roger Stafford" wrote in message <jm9d5u$het$1@newscl01ah.mathworks.com>...

>
> My use of the term "general form" referred to the case of n non-negative integers restricted to the range 0:m whose sum is s, which I believe is a much harder case that probably cannot be vectorized.

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jmb9di$pul$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <jm9d5u$het$1@newscl01ah.mathworks.com>...
>
> >
> > My use of the term "general form" referred to the case of n non-negative integers restricted to the range 0:m whose sum is s, which I believe is a much harder case that probably cannot be vectorized.
>
> I agree with you Roger. The upper bound introduced is a complication.
>
> Bruno

Bruno, found this answer and it helps me tremendously... a follow up question, is there an easier way (no for loop) to make, say "P" of these? right now I'm doing

What is a watch list?

You can think of your watch list as threads that you have bookmarked.

You can add tags, authors, threads, and even search results to your watch list. This way you can easily keep track of topics that you're interested in. To view your watch list, click on the "My Newsreader" link.

To add items to your watch list, click the "add to watch list" link at the bottom of any page.

How do I add an item to my watch list?

Search

To add search criteria to your watch list, search for the desired term in the search box. Click on the "Add this search to my watch list" link on the search results page.

You can also add a tag to your watch list by searching for the tag with the directive "tag:tag_name" where tag_name is the name of the tag you would like to watch.

Author

To add an author to your watch list, go to the author's profile page and click on the "Add this author to my watch list" link at the top of the page. You can also add an author to your watch list by going to a thread that the author has posted to and clicking on the "Add this author to my watch list" link. You will be notified whenever the author makes a post.

Thread

To add a thread to your watch list, go to the thread page and click the "Add this thread to my watch list" link at the top of the page.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

About Newsgroups, Newsreaders, and MATLAB Central

What are newsgroups?

The newsgroups are a worldwide forum that is open to everyone. Newsgroups are used to discuss a huge range of topics, make announcements, and trade files.

Discussions are threaded, or grouped in a way that allows you to read a posted message and all of its replies in chronological order. This makes it easy to follow the thread of the conversation, and to see what’s already been said before you post your own reply or make a new posting.

Newsgroup content is distributed by servers hosted by various organizations on the Internet. Messages are exchanged and managed using open-standard protocols. No single entity “owns” the newsgroups.

There are thousands of newsgroups, each addressing a single topic or area of interest. The MATLAB Central Newsreader posts and displays messages in the comp.soft-sys.matlab newsgroup.

How do I read or post to the newsgroups?

MATLAB Central

You can use the integrated newsreader at the MATLAB Central website to read and post messages in this newsgroup. MATLAB Central is hosted by MathWorks.

Messages posted through the MATLAB Central Newsreader are seen by everyone using the newsgroups, regardless of how they access the newsgroups. There are several advantages to using MATLAB Central.

Use the Email Address of Your Choice
The MATLAB Central Newsreader allows you to define an alternative email address as your posting address, avoiding clutter in your primary mailbox and reducing spam.

Spam Control
Most newsgroup spam is filtered out by the MATLAB Central Newsreader.

Tagging
Messages can be tagged with a relevant label by any signed-in user. Tags can be used as keywords to find particular files of interest, or as a way to categorize your bookmarked postings. You may choose to allow others to view your tags, and you can view or search others’ tags as well as those of the community at large. Tagging provides a way to see both the big trends and the smaller, more obscure ideas and applications.

Watch lists
Setting up watch lists allows you to be notified of updates made to postings selected by author, thread, or any search variable. Your watch list notifications can be sent by email (daily digest or immediate), displayed in My Newsreader, or sent via RSS feed.

Other ways to access the newsgroups

Use a newsreader through your school, employer, or internet service provider

Pay for newsgroup access from a commercial provider

Use Google Groups

Mathforum.org provides a newsreader with access to the comp.soft sys.matlab newsgroup