Adding a constraint to my if else if statement.

2 visualizaciones (últimos 30 días)
Clifford Shelton
Clifford Shelton el 3 de Ag. de 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!
  1 comentario
Oleg Komarov
Oleg Komarov el 3 de Ag. de 2012
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?

Iniciar sesión para comentar.

Respuestas (2)

Sean de Wolski
Sean de Wolski el 3 de Ag. de 2012
Editada: Sean de Wolski el 3 de Ag. de 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:
  4 comentarios
Sean de Wolski
Sean de Wolski el 3 de Ag. de 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
Clifford Shelton el 3 de Ag. de 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.

Iniciar sesión para comentar.


Daniel Shub
Daniel Shub el 3 de Ag. de 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.
  5 comentarios
Daniel Shub
Daniel Shub el 3 de Ag. de 2012
@Oleg, good point, but .4802<=j<=.6110 will cause problems.
Oleg Komarov
Oleg Komarov el 3 de Ag. de 2012
Yes but k has only one condition.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by