MATLAB Answers


Adding a constraint to my if else if statement.

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
      elseif 0<=j<=.0687
      elseif .0688<=j<=.1880
      elseif .1881<=j<=.3286
      elseif .3287<=j<=.4801
      elseif .4802<=j<=.6110
      elseif .06111<=j<=.7223
      elseif .7224<=j<=.8050
      elseif .8051<=j<=.8696
      elseif .8697<=j<=.9156
      elseif .9157<=j<=.9455
      elseif .9456<=j<=.9671
      elseif .9672<=j<=.9797
      elseif .9798<=j<=.9874
      elseif .9875<=j<=.9931
      elseif .9932<=j<=.9962
      elseif .9963<=j<=.9984

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 Comment

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?


No products are associated with this question.

2 Answers

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!


doc histc

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

Loren's Blog: 3<x<7


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

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!

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 Shub
on 3 Aug 2012

This has been said many times before on this site:


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.


Loren's blog deals with the issues nicely.

@Oleg, good point, but .4802<=j<=.6110 will cause problems.

Yes but k has only one condition.

Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!