Asked by Clifford Shelton
on 4 Aug 2012

This has had me stumped all day. Any help is appreciated. I'm trying to do the following with an array:

1.) randomly populate an array called 'astrosRuns' with 8133 values.

2.) the possible values to populate the 'astrosRuns' array with range in values (0:16)

3.) I want each possible value of that range to have a specific probability of occurring.

4.) I also want to establish the constraint that every group of 10 values within the 'astrosRuns' array has an average value of 4.

I'm a beginner so I've developed some code (which is probably very inefficent) to attempt to tackle goals 1,2, and 3 from above. I'm totally clueless how to do 4.

What i've done so far is construct a for loop for the 8133 steps and made a bunch of if else if statements for the probabilities of each value between the range of (0:16) occurring.

**How would I then also have the program adhere to another constraint (or bias) that every unique group of 10 values will combine to have an average value of 4 (or in essence a sum of 40)**

So..the first 10 values could be (8,5,7,4,0,0,1,4,5,6) which has an avearge of 4. But the program will be smart enough to generate the 8133 values within the array to adhere to the given probabilities 'j' AND the 10 value average rule of 4.

here's my code so far:

% This for loop begins calculating the probabilities for the occurence of a given score. I set this up as a range between values that were taken from the histogram. I had to do this manually since I don't know an easier way yet. for i=1:8133; % Let j = the probability for the score value % Let k = the probability that the adjacent values will be equal

j =rand; k =rand;

if 0<=k<=.0063 astrosRuns(i+1)=astrosRuns(i);

elseif 0<=j<=.0687 astrosRuns(i+1)=0;

elseif .0688<=j<=.1880 astrosRuns(i+1)=1;

elseif .1881<=j<=.3286 astrosRuns(i+1)=2;

elseif .3287<=j<=.4801 astrosRuns(i+1)=3;

elseif .4802<=j<=.6110 astrosRuns(i+1)=4;

elseif .06111<=j<=.7223 astrosRuns(i+1)=5;

elseif .7224<=j<=.8050 astrosRuns(i+1)=6;

elseif .8051<=j<=.8696 astrosRuns(i+1)=7;

elseif .8697<=j<=.9156 astrosRuns(i+1)=8;

elseif .9157<=j<=.9455 astrosRuns(i+1)=9;

elseif .9456<=j<=.9671 astrosRuns(i+1)=10;

elseif .9672<=j<=.9797 astrosRuns(i+1)=11;

elseif .9798<=j<=.9874 astrosRuns(i+1)=12;

elseif .9875<=j<=.9931 astrosRuns(i+1)=13;

elseif .9932<=j<=.9962 astrosRuns(i+1)=14;

elseif .9963<=j<=.9984 astrosRuns(i+1)=15;

else .9985<=j<=1; astrosRuns(i+1)=16; end end

ANY help is most appreciated. Please keep in mind that I'm a beginner...so if you could please explain any answers with a TON of detail..I just might understand.
**Please answer as if I'm the dumbest programmer you've ever met!**

thanks!

*No products are associated with this question.*

Answer by Daniel Shub
on 4 Aug 2012

I am going to give you the same answer I gave you before. The line

elseif .0688<=j<=.1880

does not do what you think it does.

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 7 Comments

## Image Analyst (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/45246#comment_93033

For (1) and (2), why can't you just do astroRuns = 16 * rand(8133, 1)? Do the numbers have to be integers? I'm not sure what (3) means. For (4), does the group of 10 move over in "jumps" of 10 elements (like 1-10, 11-20, 21-30, etc.) or in jumps of 1 (like 1-10, 2-11, 3-12, etc.)? And if you're requiring that the mean is 4 and you move by 1 so that the window overlaps, then the next value can't really be random, can it? If mean(array(1:10)) = 4 and mean(array(2:11)) = 4, then array(11) must equal array(1) or else you won't have the same mean for the two window locations.

## Clifford Shelton (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/45246#comment_93038

For (1) and (2) yes the numbers have to be integers. I am trying to simulate the runs scored by a baseball team (the Astros). So the number of runs that the Astros can possibly score in one game is between (0:16).

For (3) I mean that I want to include in the program a % probability of the Astros scoring a specific number of Runs. For example, a 6% chance of the team scoring 0 runs, a 12% chance of the team scoring 1 run...so on and so on.. (that is what my for loop with the range of values for 'j' is attempting to do in my code above.

(4) the group moves over in "jumps" of 10 elements.

## Sven (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/45246#comment_93065

Please note all, this is a twice-duplicated question from here and here.

Clifford, at the first link it has been described why it's better (ie, easier to understand) to represent your problem an an N-by-10 set of observations.

At the second link it has been described that "0<=k<=.0063 does not do what you think it does", and a number of solutions were posed.

Since this is the 3rd attempt at describing the problem, perhaps it would be helpful to take on board the advice given in the first two questions and update your question/code accordingly. Think of tackling small (well-defined) parts of your problem at a time rather than trying to do the whole (unclearly defined) thing at once in three duplicate questions.

For instance, understand the ambiguity between requiring that "each

10 numbersmust average 4", yet having a total of8133numbers? 8133 is not a multiple of 10, so the very first part of your question is impossible to address.## Oleg Komarov (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/45246#comment_93067

Im gonna delete my answer from his previous posts and refrain answering him again.

## Clifford Shelton (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/45246#comment_93091

wow. I honestly don't mean to frustrate everyone. Some bits and pieces of suggestions were given...however it seemed that the meat of my question (how do I BOTH keep a moving average of 4 for each group of 10 AND keep the probabiliitess for 'j' the same) wasn't answered.

Maybe it was...but as I've tried to state repeatedly...I'm a beginner!!!! So small snippets of code with abbreviations isn't going to sink in with me. Unfortunately, I'd need very detailed responses to really get it.

So...I did notice the suggestion of putting the data set into 'bins' of 10.

Would anyone be kind enough to explain to me exactly how that is done...then maybe I can tackle the moving average part. And let's just change the size of the data set to 8130 for ease.

Once again...sorry for being a beginner!! I really appreciate the help from everyone: Sven, Oleg, Image Analyst Woski etc....

## Sven (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/45246#comment_93103

Ok, so here's my suggestion: when you describe that the

meatof your problem is "how to BOTH keep a moving average of 4 AND keep the probabilities the same", it is clear thatyou are trying to do too much at once, especially for a beginner (and there's nothing wrong with being a beginner).I haven't even seen you take one of the (multiple) suggestions for "keep a moving average of 4", and show that "I've got this far and my code works, but I'd really like to do something slightly different".

So, here's your task: show that you've got the

firstof your requirements under control. Show us that you understood the responses and can make a matrix with 813 sets of 10 numbers each averaging 4.If you didn't understand the responses,

that's ok!Just ask aboutthatbefore making another question - it won't make it any easier to understand if you only show your original non-working code and just make the question more complex.## Image Analyst (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/45246#comment_93119

I don't think you can require that a window of 10 scores can average 4 and at the same time have each of the 10 scores be a random integer between 1 and 16 with a certain probability for each number. You can easily specify a certain probability for 16 numbers by getting a random number between 0- and 1 and chopping that interval into 16 chunks of different widths:

and so on. But the average will not necessarily be an integer and it's even less likely to be 4. In fact you'd have to force the last, 10th, element to be a certain number to get the mean to be 4 and that number almost certainly

won't be an integer, and it certainlywon't be random at all. It would be a predetermined floating point number - ithas to bein order to get the mean of 4.