If else If statment embedded in a for loop with a constraint?

It's not easy being a beginner!
Ok..I'm trying to add some bias to the following random walk in values. Here's the example code:
for i=1:20;
% Let j = probability for specific value
% Let k = the probability that two adjacent values will be equal
j =rand;
k =rand;
if 0<=k<=.0063
value(i+1)=value(i);
elseif 0<=j<=.0687
value(i+1)=0;
elseif .0688<=j<=.1880
valuei+1)=1;
elseif .1881<=j<=.3286
value(i+1)=2;
How would I add a constraint (or bias) the above random walk so that every 10 adjacent values (of the 20 random values that will be generated by the above code) must maintain a moving average with the value of 1?
I hope i'm clear on what I'm trying to do. Thanks for all the help in advance!

3 Commenti

I am sorry but i cant understand what you asking? what is your aim there? are you trying to get a random number between A and B.
or you just want that for each 10 random number created the sum of them must be 1.?
Ok. The for loop will create 20 random numbers. I want every 10 adjacent numbers together to have an average equal to 1. Example: I want {(i(1)+i(2)+i(3)+i(4)....)/10=1}
So i'm trying to create a constraint (or bias) within the random number generation.
I hope that is clearer. Sorry for the confusion
I posted some code on your duplicate question where you already received lots of feedback: http://www.mathworks.co.uk/matlabcentral/answers/45204-adding-a-constraint-to-my-if-else-if-statement
The moving average condition comes out now.
Please, formulate the condition concisely and clearly in your old post before duplicating it.

Accedi per commentare.

Risposte (1)

Sven
Sven il 3 Ago 2012
Modificato: Sven il 3 Ago 2012
Hi Clifford, Your description (from the comments above) will only happen if the second 10 numbers are an exact copy of the first 10 numbers. Think of it this way:
Imagine your first 10 random numbers add up to 10. Numbers 2:11 have simply dropped the first number and added the 11th number. If the sum of these must also add up to 10, this will only happen if i(11)==i(1), and then i(12)==i(2) for the next set, etc.
That being said, here's how I would do it:
% Make the first 10 random numbers
randSet = rand(10,1);
% Force them (by scaling) to add up to 10
randSet = randSet * (10/sum(randSet));
% Turn them into 20 numbers (by copying, as per my description above)
finalRandSet = [randSet; randSet];
% See that all windows of 10 adjacent numbers equal 10
sum(finalRandSet(1:10))
sum(finalRandSet(5:14))
sum(finalRandSet(9:18))
Did this help you out Clifford?

6 Commenti

Ok. Let me give a more detailed example. Because I haven't been clear enough. I want to make a constraint on the average in values, not the sum.
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 the above 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).
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!
If the average of 10 numbers is 4, its sum must be 40. When you specify how many numbers you have and you specify their average, you are also specifying their sum. I was using sum in my example because it more directly illustrates the point I'm making that the only way to have a moving average of n numbers equal to some value is to repeat the first n numbers. Let's do the same thing with average:
Take your first 10 values as you typed them above:
(1,4,6,6,2,2,9,4,2,4)
These are indices 1 to 10, and their values have an average of 4.
Next, move your "window" by one index. In other words, consider indices 2 to 11. They will include all but the first index of the list above:
(4,6,6,2,2,9,4,2,4,x)
Now, in order for you to maintain an average of 4 with this set, what must the value x be above?
You'll find that it must be 1 to maintain your average. Do it again for indices 3 to 12 and your next value must be 4. It's not a coincidence that these two values (1 and 4) are actually the first two values in your list.
The question definition aside, I believe we can help you to MATLAB-ify the code you wrote. MATLAB is very good at avoiding loops when dealing with numbers, and your loop above (and also the multi-if-else-statement) is a prime target.
Did you know you can make 8133 random numbers, and then ask if they're all less than 0.0063 at once? This way doesn't need a loop (and is actually much more efficient without the loop).
randNums = rand(1,1833);
mask = randNums < 0.0063;
ok! Thanks for the clarification. I now understand what you are saying. However, I want to analyze the data set by specific groups of 10. So using my example above, the data set would be: (1,4,6,6,2,2,9,4,2,4,8,5,7,4,0,0,1,4,5,6).
The average of the first 10 indices is 4 (1,4,6,6,2,2,9,4,2,4). Also the average of the next group of 10 indices is also 4 (8,5,7,4,0,0,1,4,5,6).
I'm trying to make the code from my original question generate the values for 'astrosRuns' randomly within the constraint of every group of 10 indices have an average of 4.
Any clue as how to do this? Thanks for all your help!!
So your list above is not "a set of 20 numbers"... instead it is "two sets of 10 numbers" and the first set of 10 is completely independent of the second set of 10. Your example would be better described as this:
"I have an N-by-10 matrix of random numbers. I want the average of each row of 10 numbers to equal 4".
% Make N sets of 10 random numbers
N = 5;
randSet = rand(N,10);
% Force rows (by scaling) to add up to 40 (or average to 4)
randSet = bsxfun(@times, randSet, 40./sum(randSet,2));
If you want them all to be random integers it will be a tiny bit more tricky, but the same principle should apply.
Ok...that is a better way to look at what I'm trying to do. Thanks. But now i'm a little confused as to how can I after forcing the rows to add up to 40 (by scaling) then set a probabilities (or bias) for specific values that could be created "randomly".
so in other words:
for example, I want to populate a 5-by-10 matrix.
I want the average of each row of 10 numbers to equal 4.
AND I want a 10% probability that the first value is 1
a 5% probability that the first vale is 2
a 15% probability that the first value is 3
a 10% probability that the first value is 4 etc...etc...
This way I want to generate a program that can solve the problm of generating values for my matrix with strict bias of 1. having each group of 10 averaging to 4 and 2. populating the list with biased probabilities.
Thanks for all your help!!
Hi Clifford....
I'm getting a bit dizzy here watching those goalposts shift with every post...
Perhaps you should update your question with the code you have up to now and describe clearly what your (working) code does, and how it doesn't yet meet your needs.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by