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

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Clifford Shelton
on 3 Aug 2012

So the follwoing for loop will generate 8133 random values for 'astrosRuns' between the possible range of (0:16). The random values for 'astrosRuns' are generated depending upon the random values of either 'j' or 'k'.

There are 8133 steps in this for loop. I want to add the constraint that the average of every group of 10 'astroRuns' values equals 4. (that's the average in values not the sum) Meaning that a group of ten values could possibly be: (1,4,6,6,2,2,9,4,2,4). These ten steps have an average in values of 4. And then the next group of 10 values must also have an average in values of 4 however, it could be a different combination in values like: (8,5,7,4,0,0,1,4,5,6).

Here is the example code:

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

So basically, I want to have the values for 'astrosRuns' randomly created but also follow a law of average of 4.

I hope this is clearer now. Thanks for all the help!

*No products are associated with this question.*

Answer by Sean de Wolski
on 3 Aug 2012

Edited by Sean de Wolski
on 3 Aug 2012

This could be written without the `for`-loop or the `if`-statement by using `histc`.

x = rand(8033,1); edges = [0,.0687,.1880] %etc. for all of yoru values

[~, astroRuns] = histc(x,edges); %edited!

And

doc histc

for how this works. Also, to find out why what you are doing now does not work:

Show 1 older comment

Sean de Wolski
on 3 Aug 2012

Okay. First I had a small mistake that is fixed now.

What `histc` does is take a vector of numbers, x, and divides them into bins, defined by their edges. This is the same thing you are trying to do with your if/else statements. I.e. if it's greater than this edge and less than the next edge, increment this value.

So let's picture it with bigger integers:

x = [2.5 3.5 3.5 7.7]; edges = 1:10; [bins,astroRuns] = histc(x,edges)

*bins =
0 1 2 0 0 0 1 0 0 0
astroRuns =
2 3 3 7*

Sean de Wolski
on 3 Aug 2012

Now the second bin ranging from 2 to 3 has one value in it (2.5). This shows up in bins. We are not quite as worried about bins here as: "where did each value go? I.e. you increment by setting 2,3...16 to where that bin is true. I used the second output of `histc` to identify the bin corresponding to each x-value.

Welcome to MATLAB and Answers!

Clifford Shelton
on 3 Aug 2012

Ok..I think I understand what you are saying. But I have no clue how to write the code.

Do I want my edges variable to be the range of values (0:16) or do I want my edges variable to be the ranges for the probabilities of 'k' in my above code (0,.0687,.1880,.3286,...etc.)

Furthermore, I'm still clueless about how to do an average for each group of 10values.

Answer by Daniel
on 3 Aug 2012

This has been said many times before on this site:

0<=k<=.0063

does not do what you think it does. For all `k` greater than or equal to 0, this statement is false, even if `k` is less than or equal to .0063.

Show 2 older comments

## 1 Comment

## Oleg Komarov (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/45204#comment_92912

I don't understand how do you want the average rule combined with the binning. Can you elaborate on that? Because the binning takes into consideration 1 value per time while the average rule 10 values.

For example, do you want to discard the past 10 astroRuns if their average is above 4?